Why is not a multi-segment list showing the selected items displayed? MVC

I switched and then returned to this, but I still canโ€™t get it to work.

var companiesList = subcontractRepository.SubcontractCompanies(Subcontract.subcontract_id); IEnumerable<Guid> selectedList = companiesList.Select(a => a.Id); Companies = new MultiSelectList(companiesList, "Id", "Name", selectedList); 

In SubcontractRepository.cs

  public class SelectCompanyItem { public string Name { get; set; } public Guid Id { get; set; } } public IEnumerable<SelectCompanyItem> SubcontractCompanies(Guid id) { return from c in db.companies select new SelectCompanyItem { Name = c.company_name, Id = c.company_id }; } 

View:

  <p> <label for="Companies">Company:</label> <%= Html.ListBox("Companies", Model.Companies) %> <%= Html.ValidationMessage("Companies", "*") %> </p> 

Html produced:

  <p> <label for="Companies">Company:</label> <select id="Companies" multiple="multiple" name="Companies"><option value="4cf411d0-e111-488b-822f-ea194951cfda">Second Company</option> <option value="1c21e613-a668-4817-bf6d-73befb8c9dbd">Test Company</option> </select> </p> 
+4
source share
2 answers

I have found a solution. ListBox must have a different name from MultiSelectList. I renamed MultiSelectList to my source code and it works. I donโ€™t even want to think about how much time I spent on it!

+7
source

Right now the ugly job. Set ViewData with the values โ€‹โ€‹you want to select.

 ViewData["Companies"] = new string[] { "guid-1", "guid-2" }; 

I'm still trying to debug and see why this is happening. Surprisingly, the Unit test for this use case in an MVC project works fine.

+1
source

Source: https://habr.com/ru/post/1299479/


All Articles