Hi i have a C# model class. This class i need to pass as json response by setting his property . one property name Product of this class have type of another product class, when there is no data for Product i am getting all inner property value as blank but instead of that i want blank json property .
For Exp My Class is
public class Profile_BO
{
public int Id { get; set; }
public string Username { get; set; }
public Product product{ get; set; }
public class Product
{
public int Id { get; set; }
public string Type { get; set; }
}
}
i am initializing this class from C# data table like below : -
Profile_BO profile_BO = new Profile_BO();
foreach (DataRow dr in result.Tables[0].Rows)
{
profile_BO.Id = Convert.ToInt32(dr[0]);
profile_BO.Username = Convert.ToString(dr[1]);
}
Product product = new Product();
foreach (DataRow dr1 in result.Tables[1].Rows)
{
product.Id = Convert.ToInt32(dr1[0]);
product.Type = Convert.ToString(dr1[1]);
}
profile_BO.product = product;
Finally when i am passing as a response to method : -
public async Task<HttpResponseMessage> GetUserInfo(Profile_Request profile_Request)
{
return request.CreateResponse(HttpStatusCode.OK, profile_BO);
}
And when calling on client side i am getting response json if data is present in table like : -
{
"Id": "1",
"Username": "abc",
"product": {
"Id": "232",
"Type": "34cvdcbv"
}
}
But when i have no data in product table i am getting below : -
{
"Id": "1",
"Username": "abc",
"product": {
"Id": 0,
"Type": ""
}
}
But if no data i want output like below : -
{
"Id": "1",
"Username": "abc",
"product": {}
}
One other question is : - Is it right way for binding response model from dataset ?
Idis of typeInt32and its default value is 0. You should check if if you have products first and initialize laterproduct : {}