5 c# forms asp.net-mvc http-post asp.net-mvc-3
我想发布一个具有网格布局的表单数据,每行中的一列包含下拉列表.下拉列表中的选定值映射到该行的项目ID.
我想知道在这种情况下将此数据发布到控制器操作的不同方法是什么?
作为单个参数传递已被忽略选项,因为我的表单将具有动态数据,并且它可能具有n个记录.我这个想法是否正确?
想到FormCollection,这是正确的选择吗?
像往常一样,我将从定义视图模型开始:
public class MyViewModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后有一个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" />
}
Run Code Online (Sandbox Code Playgroud)
并在相应的编辑器模板(~/Views/Shared/EditorTemplates/MyViewModel.cshtml
)中:
@model MyViewModel
<tr>
<td>
@Html.DropDownListFor(
x => x.SelectedValue,
new SelectList(Model.Values, "Value", "Text")
)
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
最后这将发布选定的值:
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model)
{
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2204 次 |
最近记录: |