我需要额外的眼睛才能看到:
如何检查以便我只保存要更改的行。
[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; }
}
答案 0 :(得分:1)
将您的模型更改为@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()
至于“如何检查以便我只保存正在更改的行”,所有控件都将被回发,因此您需要将发布的值与原始值进行比较。控制器。