我正在尝试使用 ajax 将我的模型值从 View 传递到 Controller JQuery
。在我的控制器中,该模型显示为null
。
控制器已从 ajax 方法调用,但问题是控制器中的对象显示为null
.
这是模型的简单结构:
public class Invoice
{
public Invoice(InvoiceHeader invHeader, List<InvoiceItems> InvItmem)
{
this.invHeader = invHeader;
this.InvItmem = InvItmem;
}
public InvoiceHeader invHeader { get; private set; }
public List<InvoiceItems> InvItmem { get; private set; }
}
这是控制器的结构:
[HttpPost]
public ActionResult InsertItems(List<Invoice> invoice)
{
//List<InvoiceItems> asd = new List<InvoiceItems>();
//asd = invoice.();
return Json(true, JsonRequestBehavior.AllowGet);
}
这是视图:
@model BeetaTechModel.Invoice
@{
ViewBag.Title = "Index";
var val = Json.Encode(Model);
}
<h2>Index</h2>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSubmit").click(function () {
// var val = Json.Encode(Model);
var check = @Html.Raw(val);
$.ajax({
type: 'POST',
url: "Invoice/InsertItems",
data: '{info:' + JSON.stringify(check) + '}' ,
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (data) {
alert(data);
}
});
});
});
</script>
@{Html.RenderPartial("InvoiceHeader", Model.invHeader);}
@{Html.RenderPartial("InvoiceItems", Model.InvItmem);}
<input type="submit" id="btnSubmit" value="Log In" />