MVC Multi select不会返回ViewModel中的选定项目

时间:2015-03-24 02:31:01

标签: asp.net-mvc razor asp.net-mvc-5 multi-select

我很难获得我的MVC 5多选控件以返回我的ViewModel中的所选项目。就好像所选的列表框项目没有绑定到可用的 EmployeeCategoriesId 集合一样,导致返回集合始终为null。

视图模型

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会填充传递给视图的选项。

1 个答案:

答案 0 :(得分:1)

您有一个[Bind(Include="..")]属性,该属性明确地将属性EmployeeCategoriesId排除在绑定之外。

由于您使用的视图模型应该仅代表您要在视图中编辑的属性,因此您甚至不应包含BindAttribute(默认情况下所有属性都已绑定)