ASP.NET MVC NHibernate - Bind Multiple Favorite Lists to IList <T>
How to associate a multi select combo box with a list attribute of a manually added staging table?
Classes
public class Department { public virtual int Id { get; set; } public virtual IList<Lam> Lams { get; set; } } public class Person { public virtual int Id { get; set; } public virtual IList<Lam> Lams { get; set; } } public class Lam { public virtual int Id { get; set; } public virtual Department Department { get; set; } public virtual Person Person { get; set; } } ViewModel
public class DepartmentCreateEditViewModel { public Department Department { get; set; } public IList<Person> Persons { get; set; } } Actionresult
public ActionResult Create() { // Get all Persons var persons = repositoryPerson.GetAll(); // Create ViewModel var viewModel = new DepartmentCreateEditViewModel() { Department = new Department(), Persons = persons }; // Display View return View(viewModel); } Create view
I tried to add a listbox like this.
@Html.ListBoxFor(model => model.Department.Lams, new SelectList(Model.Persons, "Id", "Name"), new { @class = "form-controll" }) To save an object, I want to return a department object
[HttpPost] public ActionResult Create(Department department) Linking to a drop-down list (with people) in IList does not work. How am I supposed to do this? Is it possible?
[Change] Code after Eric's suggestion
Create ActionResult
[HttpPost] public ActionResult Create(DepartmentCreateEditViewModel viewModelPostBack) View
@Html.ListBoxFor(model => model.Department.Lams, new MultiSelectList(Model.Persons, "Id", "Name"), new { @class = "form-controll" }) What will I be back:
viewModelPostBack {EmpLaceMgmt.ViewModels.DepartmentCreateEditViewModel} Department: {EmpLaceMgmt.Models.Department} Persons: null viewModelPostBack.Department {EmpLaceMgmt.Models.Department} Id: 0 Lams: Count = 0 The generated HTML is as follows
<select class="form-controll" id="Department_Lams" multiple="multiple" name="Department.Lams"> <option value="1">Example Person</option> </select> You have three problems. Firstly, you are trying to bind to IList<T> , but this will not work, because the modelβs middleware will not know which specific object it should create to satisfy this ... There are many objects that support IList<T> , so which one?
Secondly, you need to use a MultiSelectList , not a SelectList in your helper.
Third, you submit a different type of model than you use to create your pages. And this type has a completely different structure. In the structure with which you create your page, your data is created with the name Department.Lams (because Department is a property of your ViewModel), but your Post action uses the Department model, which would be a binder to look for an object simply called Lams , not Department.Lams .
So, convert your models to a specific type, such as List<Lam> , and then go back to your ViewModel, not the department, and extract the department from ViewModel and finally replace the helper with this:
@Html.ListBoxFor(model => model.Department.Lams, new MultiSelectList(Model.Persons, "Id", "Name"), new { @class = "form-controll" })