我有一个视图,它迭代模型列表并为列表中的每个模型调用editorfor。该列表只是可以修改的一行数据。如何将此列表发回给控制器并进行更改?主要观点是:
@model List<Program.Models.Model>
@{
ViewBag.Title = "DPR";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("CreateDpr", "Home", FormMethod.Post, new { @id = "formDPR"}))
{
<div>
<table>
<tr>
<td>
<button class="displaybutton" onclick="CreateDPR();">Create DPR</button>
</td>
</tr>
</table>
</div>
<div style="background-color:white">
<table style="font-size:xx-small" class="table_body">
<caption style="font-size:medium;font-weight:bold">Patients For DPR</caption>
<thead>
<tr style="background-color:lightgray">
<th>Name</th>
<th>ID</th>
<th>Last Review</th>
<th>Include?</th>
<th>Reason</th>
<th>Notes</th>
</tr>
</thead>
@for (var i = 0; i < Model.Count; i++)
{
<tr>
@Html.EditorFor(m=>Model[i])
</tr>
}
</table>
</div>
<div>
</div>
}
JS功能:
$.ajax({
type: 'POST',
data: $("#formDPR").serializeArray(),
url: '@Url.Action("CreateDpr")',
success: function (data) {
var test = data;
}
});
return false;
控制器:
[HttpPost]
public ActionResult CreateDpr(List<Model> models)
{
var pats = models;
return Json("DONE");
}