我必须InvoiceViewModel
通过 ajax 将数据从视图发送到操作方法。但是,我收到 500 内部服务器错误代码。
查看我需要从视图发送到控制器的模型详细信息
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; }
}
这是我的控制器。它直接位于根目录中。我正确地提供了 ajax 调用的 url。
public class HomeController : Controller
{
[HttpPost]
public ActionResult SaveInvoice(InvoiceViewModel invoiceDetails)
{
ViewBag.Message = "Your application description page.";
return View();
}
}
如果我如下更改操作方法的参数类型。是击球动作法。但是,invoiceDetails 为空
[HttpPost]
public ActionResult SaveInvoice(string invoiceDetails)
{
ViewBag.Message = "Your application description page.";
return View();
}
这是ajax调用代码。
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");
}
});
}
谁能告诉我我在这里做错了什么。
谢谢