我有以下问题。我需要在分组列表中编辑项目值,并将模型发布到服务器。如何使用编组列表中的已编辑项目发布模型?
我的模型和视图模型:
public class MyObject
{
public string ParentName { get; set; }
public string SubParentName { get; set; }
public string ItemName { get; set; }
public decimal Value { get; set; }
}
public class MyViewModel
{
public IList<MyObject> list;
}
而且简化(不分组的项目中的HTML表格组)实现的观点:
@model MyViewModel
@(using (Html.BeginForm("Action", "Controller", FormMethod.Post)))
{
<table>
<tbody>
@foreach(var parentItems in Model.list.GroupBy(x => x.ParentName))
{
foreach(var subParents in parentItem.GroupBy(x => x.SubItemName))
{
foreach (var item in subParents)
{
<tr>
<td><parentItems.FirstOrDefault().ParentName</td>
<td><subParents.FirstOrDefault().SubParentName</td>
<td>item.ItemName</td>
<td><input class="itemValue">**Editable value item.Value**</td>
</tr>
}
}
}
</tbody>
</table>
<button type="submit">Save</button>
}
怎么办呢?
解决
@model MyViewModel
@(using (Html.BeginForm("Action", "Controller", FormMethod.Post)))
{
<table>
<tbody>
@foreach(var parentItems in Model.list.GroupBy(x => x.ParentName))
{
@{ var counter = 0;}
foreach(var subParents in parentItem.GroupBy(x => x.SubItemName).Select(x => x.ToList()))
{
for(int i = 0; i < subParents.Count(); i++, counter++)
{
<tr>
<td>
@Html.DisplayFor(x => x.list[counter].ItemName)
@Html.HiddenFor(x => x.list[counter].ItemName)
</td>
<td>
@Html.TextBoxFor(x => x.list[counter].Value, new { myAttr = "myAttrValue"});
</td>
</tr>
@Html.HiddenFor(x => x.Reports[counter].ParentName)
@Html.HiddenFor(x => x.Reports[counter].SubParentName)
}
}
}
</tbody>
</table>
<button type="submit">Save</button>
}
感谢大家的帮助:)
我会建议使用'editortemplate'为此 – Amit 2014-09-25 07:44:33
EditorFor绑定到模型。我想从模型列表中编辑项目。 – christo 2014-09-25 07:49:14
你想编辑项目值? – Amit 2014-09-25 07:50:27