[英]Another null collection being passed to MVC Controller
我需要其他眼睛才能看到:
如何检查以便仅保存要更改的行。
[HttpPost] public ActionResult Index(IList<EZone_ServiceGroup> sgList) { try { foreach (EZone_ServiceGroup sg in sgList) svcGroupRepo.UpdateServiceGroup(sg); return RedirectToAction("Index"); } catch { return View(); } }
视图:
@model IEnumerable<KTCEzone.Domain.Entities.EZone_ServiceGroup>
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
<div class="row">
<table class="table table-condensed table-bordered table-hover table-striped small" id="sgTable">
<tr>
<th class="col-sm-12">@Html.DisplayNameFor(model => model.GroupID)</th>
<th>@Html.DisplayNameFor(model => model.GroupName)</th>
<th>@Html.DisplayNameFor(model => model.ParentGroupID)</th>
<th>@Html.DisplayNameFor(model => model.Active)</th>
<th>@Html.DisplayNameFor(model => model.OrderIndex)</th>
</tr>
@{var items = Model.ToArray();}
@for (int i = 0; i < items.Length; i++)
{
<tr>
<td>@Html.DisplayFor(modelItem => items[i].GroupID)</td>
<td>@Html.EditorFor(modelItem => items[i].GroupName) </td>
<td>@Html.EditorFor(modelItem => items[i].ParentGroupID) </td>
<td>@Html.CheckBoxFor(modelItem => items[i].Active) </td>
<td>@Html.EditorFor(modelItem => items[i].OrderIndex) </td>
</tr>
}
</table>
</div>
}
模型:
public class EZone_ServiceGroup
{
public int GroupID { get; set; }
public string GroupName { get; set; }
public bool Active { get; set; }
public int OrderIndex { get; set; }
public int ParentGroupID { get; set; }
}
将模型更改为@model IList<KTCEzone.Domain.Entities.EZone_ServiceGroup>
,然后从视图中删除@{var items = Model.ToArray();}
并使用
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>@Html.DisplayFor(m => m[i].GroupID)</td>
<td>@Html.EditorFor(m=> m[i].GroupName)</td>
<td>@Html.EditorFor(m=> m[i].ParentGroupID)</td>
<td>@Html.CheckBoxFor(m=> m[i].Active) </td>
<td>@Html.EditorFor(m=> m[i].OrderIndex) </td>
</tr>
}
它将正确命名您的元素。 如果无法将集合更改为IList,则需要使用自定义EditorTemplate
作为模型的类型,并与@Html.EditorFor()
结合使用
至于“如何检查以便只保存被更改的行” ,所有控件都将被回发,因此您需要将已发布的值与控制器中的原始值进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.