我想发布一个具有网格布局的表单数据,每行中的一列包含下拉列表。下拉列表中选择的值映射到该行的项ID。
我想知道在这种情况下将此数据发布到控制器操作的不同方法是什么?
将其作为单个参数传递已被忽略,因为我的表单将具有动态数据,并且可能会有n条记录。这样想对吗?
考虑使用FormCollection,这是正确的选择吗?
我想发布一个具有网格布局的表单数据,每行中的一列包含下拉列表。下拉列表中选择的值映射到该行的项ID。
我想知道在这种情况下将此数据发布到控制器操作的不同方法是什么?
将其作为单个参数传递已被忽略,因为我的表单将具有动态数据,并且可能会有n条记录。这样想对吗?
考虑使用FormCollection,这是正确的选择吗?
一如既往,我会先定义一个视图模型:
public class MyViewModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
然后有一个GET控制器动作,将填充此视图模型的集合,并在相应的视图中使用编辑器模板:
@model IEnumerable<MyViewModel>
@using (Html.BeginForm())
{
<table>
<thead>
<tr>
<th>Some column name</th>
</tr>
</thead>
<tbody>
@Html.EditorForModel()
</tbody>
</table>
<input type="submit value="OK" />
}
~/Views/Shared/EditorTemplates/MyViewModel.cshtml
):@model MyViewModel
<tr>
<td>
@Html.DropDownListFor(
x => x.SelectedValue,
new SelectList(Model.Values, "Value", "Text")
)
</td>
</tr>
最后,这将发布所选的值:
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model)
{
...
}