从那时起,我一直试图解决它整整两天。我基本上已经阅读了与这种情况相关的所有StackOverflow主题,但没有一个能够解决我的问题(对Razor / MVC安静失望:太原始/原始)。
首先要做的事情是:我将构建一个工作流程定义页面。 此定义由动态列表组成:批准级别,用户和角色。
查看模型
public class FlowViewModel
{
public IList<SelectListItem> UserList {get; set;}
public IList<SelectListItem> RoleList {get; set;}
public IEnumerable<SelectListItem> SelectedUser {get; set}
public IEnumerable<SelectListItem> SelectedRole {get; set}
}
控制器 - 索引(GET)
public ActionResult Index()
{
FlowViewModel flowViewModel = new FlowViewModel();
//Logic that gathers the user list and role list from database and set
//the underlying lists (FlowViewModel.UserList and FlowViewModel.RoleList).
return View(flowViewModel)
}
查看
@model App.Models.FlowViewModel
@Html.BeginForm("CreateFlow", "Flow")
{
@Html.DropDownListFor(m => m.SelectedUSer, Model.UserList, "SELECT")
@Html.DropDownListFor(m => m.SelectedRole, Model.RoleList, "SELECT")
}
控制器 - 发布
public ActionResult CreateFlow(FlowViewModel flowVM)
{
//whatever
}
通过这样做,我正确地接收了给定列表的选定索引(在FlowViewModel中)。
主要问题:
e.g:
查看
@Html.BeginForm("CreateFlow", "Flow")
{
@for(int i; i < ViewBag.LevelsAmount; i++)
{
@Html.DropDownListFor(m => m.SelectedUSer, Model.UserList, "SELECT")
@Html.DropDownListFor(m => m.SelectedRole, Model.RoleList, "SELECT")
}
<input type="submit">OK</input>
}
CONTROLLER(POST)
public ActionResult CreateFlow(IList<FlowViewModel> flowVMList)
{
//whatever
}
答案 0 :(得分:0)
您需要一遍又一遍地绑定到数组/列表而不是相同的属性