0

I am working on a universal windows 10 application. I am working with a webAPI and the I am trying to make a post call using JSON serialization of my class, The code is as follows:

public async void PostAPI()
    {
        try
        {
            var postObject = JsonConvert.SerializeObject(thisUser);

            string _serviceUrl = Constants.BaseUrl + "api/Account/Register";
            HttpClient client = new HttpClient();

            await client.PostAsync(new Uri(_serviceUrl), new HttpStringContent(postObject, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"));
        }
        catch (Exception)
        {
            throw;
        }

    }

the values for thisUser are being taken from textbox's in the UI something like this:

        RegisterModel thisUser = new RegisterModel();
thisUser.UserDetails = new UserDetails();
        thisUser.Email = UserInputemail.Text;
        thisUser.Password = "addsFABBS!2";
        thisUser.ConfirmPassword = "addsFABBS!2";
        thisUser.UserDetails.FullName = UserInputName.Text;
        thisUser.UserDetails.Username = UserInputUserName.Text;
        thisUser.UserDetails.FullName = UserInputName.Text;
        thisUser.UserDetails.ICEFullName = ICEName.Text;
        thisUser.UserDetails.ICEMobileNumber = int.Parse(ICEPhoneNo.Text);
        thisUser.UserDetails.DoctorFullName = DocName.Text;
        thisUser.UserDetails.DoctorMobileNumber = int.Parse(DocPhoneNo.Text);

The postObject remains null. it has the value {} where as in the thisUser the values are present.

the thisUser is an instance of the RegisterModel class which has the following properties:

  using System.Runtime.Serialization;
  namespace APIValueSetterTest.Model
  {
    [DataContract]
    public class RegisterModel
    {
        public int UserId { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }

        public UserDetails UserDetails { get; set; }
    }

}

and the user details class is as follows:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
{
    [DataContract]
    public class UserDetails
    {
        public int UserId { get; set; }

        public string FullName { get; set; }

        public string Username { get; set; }

        public string ICEFullName { get; set; }

        public int ICEMobileNumber { get; set; }

        public string DoctorFullName { get; set; }

        public int DoctorMobileNumber { get; set; }
    }
}

Where am I going wrong?

0

1 Answer 1

2

I think you need to indicate the properies on this class to be data members, like so:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
  {
    [DataContract]
    public class RegisterModel
    {
        [DataMember]      
        public int UserId { get; set; }
        [DataMember]      
        public string Email { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string ConfirmPassword { get; set; }
        [DataMember]
        public UserDetails UserDetails { get; set; }
    }

}

Same goes for the UserDetails class:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
{
    [DataContract]
    public class UserDetails
    {
        [DataMember]
        public int UserId { get; set; }
        [DataMember]
        public string FullName { get; set; }
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string ICEFullName { get; set; }
        [DataMember]
        public int ICEMobileNumber { get; set; }
        [DataMember]
        public string DoctorFullName { get; set; }
        [DataMember]
        public int DoctorMobileNumber { get; set; }
    }
}

Properties not decorated with the DataMember attribute will result in them being ignored by the serializer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.