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?