MVC Multi select 不会 return select 编辑 ViewModel 中的项目
MVC Multi select doesn't return selected items in ViewModel
我很难让我的 MVC 5 multi select 控制 return 我的 ViewModel 中的 selected 项目。就好像 selected 列表框项目没有绑定到可用的 EmployeeCategoriesId 集合,导致 return 集合始终为空。
ViewModel
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 中的 returning "EmployeeCategoriesId" 总是 returning null 到 POST 控制器操作吗?
ListBox 确实填充了传递给视图的选项。
您有一个 [Bind(Include="..")]
属性,它专门从绑定中排除了 属性 EmployeeCategoriesId
。
由于您使用的是视图模型,它应该只表示您要在视图中编辑的属性,因此您甚至不应该包含 BindAttribute
(默认情况下所有属性都已绑定)
我很难让我的 MVC 5 multi select 控制 return 我的 ViewModel 中的 selected 项目。就好像 selected 列表框项目没有绑定到可用的 EmployeeCategoriesId 集合,导致 return 集合始终为空。
ViewModel
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 中的 returning "EmployeeCategoriesId" 总是 returning null 到 POST 控制器操作吗? ListBox 确实填充了传递给视图的选项。
您有一个 [Bind(Include="..")]
属性,它专门从绑定中排除了 属性 EmployeeCategoriesId
。
由于您使用的是视图模型,它应该只表示您要在视图中编辑的属性,因此您甚至不应该包含 BindAttribute
(默认情况下所有属性都已绑定)