I am developing MVC application.
I want to submit the form using ajax function, not using standard submit button click event.
I want to pass object of the model along with string array (which is not related to object of the model.)
I have below code in Create View
@using (Html.BeginForm("Create", "AdviceCreate", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate", onsubmit = "disableSubmitButton()" }))
{ }
$('#create').click(function () {
alert("create")
var count = $(".clsInvoiceId").length;
var invoiceDetailsArray = new Array();
var ledgerDetailsArray = new Array();
var obj = '@Html.Raw(Json.Encode(Model))';
$.ajax({
contentType: 'application/json; charset=utf-8',
url: '@Url.Action("Create", "AdviceCreate")',
type: 'POST',
data:
JSON.stringify({
AdviceCreateVM : obj,
InvoiceDetails : invoiceDetailsArray,
LedgerDetails : ledgerDetailsArray
}),
success: function () {
}
});
});
and below code in controller
public ActionResult Create(AdviceCreateVM AdviceCreateVM, string[] InvoiceDetails, string[] LedgerDetails)
{
}
but in controller, AdviceCreateVM comes as NULL ...
what is missing ?
alert(obj)before submitting ?objcoz i guess you are outputing string and the model binder can't bind the type toAdviceCreateVMvar obj = '@Html.Raw(Json.Encode(Model))';returns?