【发布时间】:2015-03-24 02:31:01
【问题描述】:
我很难让我的 MVC 5 多选控件返回 ViewModel 中的选定项目。就好像选定的列表框项没有绑定到可用的 EmployeeCategoriesId 集合,导致返回集合始终为空。
视图模型
public class EmployeeCreateViewModel
{
[Required]
public long OrganizationId { get; set; }
[Display(Name = "FirstName", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "FirstNameRequired")]
public string FirstName { get; set; }
[Display(Name = "LastName", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "LastNameRequired")]
public string LastName { get; set; }
[Display(Name = "DateOfBirth", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "DateOfBirthRequired")]
public DateTime? DOB { get; set; }
//Employee categories selected from multi select list
public IEnumerable<int> EmployeeCategoriesId { get; set; }
//Employee categories for multi select list
[Display(Name = "Department", ResourceType = typeof(Resources.Resources))]
public List<EmployeeCategoryDefinition> SelectEmployeeCategories { get; set; }
}
控制器
// GET: Employees/Create
public ActionResult Create()
{
long organizationId = Convert.ToInt64(User.Identity.GetOrganizationId());
EmployeeCreateViewModel employeeCreateViewModel = new EmployeeCreateViewModel();
employeeCreateViewModel.SelectEmployeeCategories = db.EmployeeCategoryDefinition
.Where(ecat => ecat.OrganizationId == organizationId)
.OrderBy(ecat => ecat.Name)
.ToList();
return View(employeeCreateViewModel);
}
// POST: Employees/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,OrganizationId,FirstName,LastName,DOB,CreatedUTC")] EmployeeCreateViewModel employeeCreateViewModel)
{
if (ModelState.IsValid)
{
Employee employee = new Employee(employeeCreateViewModel);
db.Employee.Add(employee);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
//Get Employee categories from DB - List binding lost at post back
long organizationId = Convert.ToInt64(User.Identity.GetOrganizationId());
employeeCreateViewModel.SelectEmployeeCategories = db.EmployeeCategoryDefinition
.Where(ecat => ecat.OrganizationId == organizationId)
.OrderBy(ecat => ecat.Name)
.ToList();
return View(employeeCreateViewModel);
}
视图(包含在 BeginForm 中)
<div class="col-sm-10">
@Html.ListBoxFor(m => m.EmployeeCategoriesId,
new MultiSelectList(Model.SelectEmployeeCategories, "Id", "Name"),
new { @class = "chosen-select", @style = "width:350px;", @data_placeholder = "PH" })
</div>
你能明白为什么我的 ViewModel 中返回的“EmployeeCategoriesId”总是返回 null 给 POST 控制器操作吗? ListBox 会填充传递给视图的选项。
【问题讨论】:
-
您有一个
[Bind(Include="..")]属性,该属性专门从绑定中排除了属性EmployeeCategoriesId。在任何情况下,您都在使用视图模型,那么为什么会有BindAttribute? -
@StephenMuecke 太棒了!你完全正确。完全错过了这个。我花了几个小时到处玩,就这么简单。这对你来说是新手!非常感谢您的回答。
标签: asp.net-mvc razor asp.net-mvc-5 multi-select