我有以下問題。我需要在分組列表中編輯項目值,並將模型發佈到服務器。如何使用編組列表中的已編輯項目發佈模型?
我的模型和視圖模型:
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