I am trying to pass an array from a jQuery POST to a C# WebApi service, but I get the following error:
object reference not set to an instance of an object.
jQuery code:
$(document).ready(function ()
{
$("#btn").click(function ()
{
var purchases = { dealid1: ["2", "3", "4"], theatid1: "5", mouvid1: "408", bookdt1: "10/11/2015", shtime1: "11:00AM", fbid1: "100" };
$.ajax(
{
type: "POST",
data: { purchases },
url: "api/purchase",
contentType: "application/json"
});
});
});
<input type="button" value="mybtn" id="btn" />
WebApi code:
public object post([FromBody] purchase purchases)
{
return Request.CreateResponse(jsonvalues(purchases));
}
private object jsonvalues(purchase purchases)
{
object result = false;
for (int i = 0; i < purchases.dealid1.Length; i++)//I got error here//
{
int count= purchases.dealid1.Count();
dealid = purchases.dealid1;
string arr = dealid[i];
}
theatid = purchases.theatid1;
mouvid = purchases.mouvid1;
bookdt = purchases.bookdt1;
shtime = purchases.shtime1;
fbid = purchases.fbid1;
}
public class purchase
{
public string[] dealid1 { get; set; }
public string theatid1 { get; set; }
public string mouvid1 { get; set; }
public string bookdt1 { get; set; }
public string shtime1 { get; set; }
public string fbid1 { get; set; }
}
I am able to get other values like theatid1
, mouvid1
, etc. But dealid1
is not accessible. Any ideas what I'm doing wrong?