0

I need a way to set already selected values in a multi select dropdown in a edit view in a web application (ASP.NET, Mvc 5) :

 <div class="form-group">
   <div class="col-md-10">
        @Html.DropDownList("EntityId", selectList: (IEnumerable<SelectListItem>)ViewBag.EntityId, htmlAttributes: new { @class = "form-control select2", multiple = "multiple" })
   </div>
</div>

//Controller

  ViewBag.EntityId = new SelectList(context.CompanyEntity.Where(x => x.UserProfile.Any(u => u.UserProfileId == id)), "EntityId", "EntityName"); 

I need a edit view by set selecting already selected values.

  • You cannot have the same name for the property you are binding to and the `SelectList` (refer [Can the ViewBag name be the same as the Model property name in a DropDownList?](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist). Your model needs an `IEnumerable` property to bind to, and you set the value of that in the GET method before you pass the model to the view –  Oct 11 '18 at 10:35
  • And you cannot use `DropDownList` for a ` –  Oct 11 '18 at 10:37
  • thank you for your reply but i can't use Select 2 with listBox ? – El Mehdi El Mellali Oct 11 '18 at 10:46
  • Yes of course - it generates a ` –  Oct 11 '18 at 10:48
  • Thank You So Mush – El Mehdi El Mellali Oct 11 '18 at 11:11

0 Answers0