How can I get my MultiSelectList to bind to my data model?

I have many different relationships between machines and many other tables. In my opinion, I want to have a list of multiselects for elements such as lights. I tried many different formats, I can not get the list to set the selected items. My preferred way to do this would be @ Html.ListBoxFor (model => model.enitity.relatedentity, Model.SomeSelectListItemList), but I can't seem to get a list to set the selected items in the view.

Below is my code that I tested I use Asp.net MVC5, C # and framework 6 entity.

This is my action with the controller

// GET: Car/Edit/ [Authorize(Roles = "Ecar")] public ActionResult Edit(int id) { CarsEditViewModel carEditViewModel = new CarsEditViewModel(); carEditViewModel.Cars = unitOfWorkcar.CarRepository.FindIncluding(id); IList<Approval> approvalList = unitOfWorkcar.ApprovalRepository.All.ToList(); IList<Connector> connectorList = unitOfWorkcar.ConnectorRepository.All.ToList(); IList<InputVoltage> inputVoltagesList = unitOfWorkcar.InputVoltageRepository.All.ToList(); carEditViewModel.ApprovalList = from c in approvalList select new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = true}; carEditViewModel.Categories = new MultiSelectList(carEditViewModel.ApprovalList, "Value", "Text", "Selected"); // ,carEditViewModel.ApprovalList.Select(c => c.Text),carEditViewModel.ApprovalList.Select(c => c.Selected) //carEditViewModel.ApprovalList = from c in approvalList select new MultiSelectList( //{ Selected = (carEditViewModel.Cars.Approvals.Any(app => app.Id == c.Id)) , Text = c.Name, Value = c.Id.ToString() }; // carEditViewModel.ConnectorList = from c in connectorList select new SelectListItem { Selected = true, Text = c.Name, Value = c.Id.ToString() }; carEditViewModel.InputVoltageList = from c in inputVoltagesList select new SelectListItem { Text = c.Name, Value = c.Id.ToString() }; return View(carEditViewModel); } 

Here is my look

 @model NewBobPortal.ViewModels.CarsEditViewModel @using (Html.BeginForm()) { @* @Html.ListBoxFor("SelectedApprovals",model => model., new { @class = "multiselect" })*@ @*@Html.ListBoxFor(model => model.Cars.Approvals, Model.ApprovalList) @Html.ListBoxFor(model => model.Cars.Connectors,Model.ConnectorList, new {Multiple = "multiple"}) @Html.ListBoxFor(model => model.ConnectorList, Model.ConnectorList)*@ @*@Html.ListBox("test",Model.Cars.InputVoltages, Model.InputVoltageList)*@ @Html.DropDownList("somethingelse", new MultiSelectList(Model.InputVoltageList, "Value", "Text", Model.InputVoltageList.Select(c => c.Value)), new { multiple = "multiple" }) @Html.DropDownListFor(model => model.Cars.InputVoltages , new MultiSelectList(Model.LensColorList, "Value", "Text", Model.LensColorList.Select(c => c.Value)), new { multiple = "multiple" }) @Html.ListBoxFor(m => m.Cars.Approvals, Model.Categories) <p> <input type="submit" value="Save" /> </p> 

}

This is my view model.

 using NewBobPortal.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace NewBobPortal.ViewModels { public class CarsEditViewModel { public Car Cars { get; set; } public IEnumerable<SelectListItem> ApprovalList { get; set; } public IEnumerable<MultiSelectList> ConnectorList { get; set; } public IEnumerable<SelectListItem> InputVoltageList { get; set; } public MultiSelectList Categories { get; set; } public IEnumerable<Approval> SelectedCategories { get; set; } } 

}

+7
c # asp.net-mvc entity-framework asp.net-mvc-5 entity-framework-6
source share
1 answer

The biggest problem with sending values ​​for the many-to-many relationship is the lack of a direct field to bind to your model. Here, viewing models become very convenient that you are already using, but not quite right for this.

First you need a SelectList , which may just be IEnumerable<SelectListItem> . It will contain all available options, which is quite simple. So, in your view model:

 public IEnumerable<SelectListItem> CategoryChoices { get; set; } 

And in your action:

 carEditViewModel.CategoryChoices = approvalList.Select(m => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }); 

Please note that I do not install Selected : we will allow HtmlHelper to handle this. I also do not deal with MultiSelectList .

Now you will also need something to send messages back, since your values ​​will be identifiers, we will use List<int> , so in your view model:

 private List<int> selectedCategories; public List<int> SelectedCategories { get { if (selectCategories == null) { selectedCategories = Categories.Select(m => m.Id).ToList(); } return selectedCategories; } set { selectedCategories = value; } } 

Here is a bit. The set method of the property is simple: when we return the post value, just set selectedCategories for it. get little more complicated: here we need to condense the list of category objects (called Categories here because I don’t know where it comes from) into a simple list of identifiers for these categories.

Now, in your opinion:

 @Html.ListBoxFor(m => m.SelectedCategories, Model.CategoryChoices) 

That is all you need. You are using the ListBox control so that it is already a list with multiple selections. And, tying it to the list of all currently selected identifiers, he knows which elements to automatically select in the SelectListItem list, he gets from Model.CategoryChoices .

In your post, after that you need to translate these identifiers into related objects:

  var newCategories = repository.Categories.Where(m => carEditViewModel.SelectedCategories.Contains(m.Id)); 

Then you can manually set your model categories to this new list:

 car.Categories = newCategories; 
+9
source share

All Articles