I have a ASP Core Razor form with multiple objects. For simplicity's sake, let's say it's an employee object and the form allows you to edit multiple employees on the same page.
I am using a technique put the objects on the page like this solution:
https://stackoverflow.com/a/31709184/1359788
<form id="emp_frm" method="post">
...
@for (var i = 0; i < Model.Count(); i )
{
@Html.EditorFor(m => Model[i])
}
...
</form>
But I need to do an javascript(/jquery) ajax verification of some of the fields while the user is editing the form (but before it is submitted).
Normally, I would use a $("#emp_frm").serializeArray() to get the form names/values and pass them in the ajax data: field. But with the multiple objects, this doesn't seem to work. On my Ajax controller method, it looks like this:
public async Task<JsonResult> GetEmployees(IEnumerable<Employee> employees)
but it always comes in as null no matter what I have in the form. Any suggestions?
CodePudding user response:
it will never work this way , you can not use editorfor for the whole object , try this
@for (var i = 0; i < Model.Count(); i )
{
@Html.EditorFor(model => model[i].FirstName)
@Html.EditorFor(model => model[i].LastName)
....and so on
}
CodePudding user response:
You can try to use $("#emp_frm").serilalize()
rather than $("#emp_frm").serializeArray()
:
var s = $("#emp_frm").serialize();
$.ajax({
url: "GetEmployees",
type: 'post',
data: s,
success: function (response) {
//end of Ajax
}
});