I am having trouble binding incoming JSON data in the body of an HTTP POST request to my C# model.
Here is my front-end JavaScript code:
let jsonData = "{\"Updates\":[{\"CarrierStateMapGuid\":\"de4abaa8-42d2-4e00-657a08d5577ac94a\",\"QuestionTag\":\"CoQstPAVT500006\",\"MemberOf\":\"Quote\",\"Condition\":\"0\",\"QuestionType\":\"List\",\"TrueAnswer\":\"NoDiscount\",\"TrueExplanation\":\"No Discount\",\"FalseAnswer\":null,\"FalseExplanation\":null,\"DeleteRequest\":false}]}";
$.ajax({
url: "/api/CarrierQuestionMappingApi/UpdateQuestionMaps",
type: "POST",
contentType: "application/json; charset=utf-8",
data: jsonData
});
Here is my C# model:
public class UpdateCarrierQuestionMapsWebRequests
{
public UpdateCarrierQuestionMapsWebRequest[] Updates { get; set; }
public class UpdateCarrierQuestionMapsWebRequest
{
public string CarrierStateMapGuid { get; set; }
public string QuestionTag { get; set; }
public string MemberOf { get; set; }
public string Condition { get; set; }
public string QuestionType { get; set; }
public string TrueAnswer { get; set; }
public string TrueExplanation { get; set; }
public string FalseAnswer { get; set; }
public string FalseExplanation { get; set; }
public bool DeleteRequest { get; set; }
}
}
Here is my back-end C# controller code:
[HttpPost]
[Route("api/[controller]/UpdateQuestionMaps")]
public HttpResponseMessage UpdateQuestionMaps(UpdateCarrierQuestionMapsWebRequests request)
{
// request.Updates is null
}
I cannot figure out why request.Updates is null and is not getting set by the model binder.
requestparameter to beList<UpdateCarrierQuestionMapsWebRequest> updatesand getting rid ofpublic UpdateCarrierQuestionMapsWebRequest[] Updates { get; set; }