我想发布一个具有网格布局的表单数据,每行中的一列包含下拉列表。下拉列表中的选定值映射到该行的项目 ID。
我想知道在这种情况下将此数据发布到 Controller 操作的不同方式有哪些?
作为单个参数传递已被忽略选项,因为我的表单将包含动态数据并且它可能有 n 条记录。我的这种想法是否正确?
想到FormCollection,这个选择对吗?
最佳答案
一如既往,我会先定义一个 View 模型:
public class MyViewModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
然后有一个 GET Controller 操作,它将填充此 View 模型的集合,并在相应的 View 中使用编辑器模板:
@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)
{
...
}
关于c# - 将表单数据发送到 ASP.net MVC 3 中的 Controller 操作有哪些不同的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5867232/