I have to send InvoiceViewModel
data from view to action method through ajax. But, I am getting 500 Internal server error code.
View model details I need to send from view to controller
public class InvoiceViewModel
{
[DisplayName("Invoice ID")]
public string InvoiceId { get; set; }
[DisplayName("Number of Line items")]
public int LineItemCount { get; set; }
public int TotalItemCount{get;set;}
public int VendorId { get; set; }
public List<SoftwareViewModel> LstSoftwares { get; set; }
InvoiceViewModel()
{
LstSoftwares = new List<SoftwareViewModel>();
}
}
public class SoftwareViewModel
{
public string Name { get; set; }
public string Edition { get; set; }
public string Version { get; set; }
public string LicenseNumber { get; set; }
public string ExpiryDate { get; set; }
public string AssetNumber { get; set; }
}
Here is my controller.It resides directly in root directory. I am providing url of ajax call correctly.
public class HomeController : Controller
{
[HttpPost]
public ActionResult SaveInvoice(InvoiceViewModel invoiceDetails)
{
ViewBag.Message = "Your application description page.";
return View();
}
}
If I change parameter type of action method as follows. It's hitting action method. But, invoiceDetails gets null
[HttpPost]
public ActionResult SaveInvoice(string invoiceDetails)
{
ViewBag.Message = "Your application description page.";
return View();
}
Here is ajax call code.
function SaveInvoice()
{
var mydata = {
InvoiceId: "",
VendorId: 0,
LineItemCount: 0,
TotalItemCount: 0,
LstSoftwares: {}
};
var testdata = JSON.stringify(mydata);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data:testdata,
dataType: "json",
url: "/home/SaveInvoice",
success: function (resp) {
alert("success")
},
error: function () {
alert("fail");
}
});
}
Can anyone please tell me what I am doing wrong here.
Thanks