I'm try to pass my model values from View to Controller by using ajax JQuery
. In my controller that model is shown as null
.
Controller already called from ajax method, but the problem is object in the controller is showing as null
.
This is Simple structure of model:
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; }
}
This is structure of Controller:
[HttpPost]
public ActionResult InsertItems(List<Invoice> invoice)
{
//List<InvoiceItems> asd = new List<InvoiceItems>();
//asd = invoice.();
return Json(true, JsonRequestBehavior.AllowGet);
}
This is View:
@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" />
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…