Search code examples
c#asp.net-mvcrazor-pagesrazorengine

Assigning selected items to drop-down list on update "multiple"


I have an HTML.dropdown multiple select2 working perfect. When I save values,

BUT: On update page I have to show the pre selected values in the dropdown

here is the code:

<div class="col-md-6 mb-3" id="categorylist">
   <p class="mb-1 font-weight-bold text-muted mt-3 mt-md-0">Category*</p>
   @Html.DropDownList("pCategory[]", new SelectList(new admin.Models.CategoryModel().getMultipleCategoryBySP(), "cat_id", "cat_name", --placeToProvideSingleIntValue--), 
     new { @class = " form-control select2-multiple ", @data_toggle = "select2", @multiple = "multiple", @style = "width: 100%;" })
</div>

in above code, there is a place holder --placeToProvideSingleIntValue-- where I can place single integer value it shows as preSelected.

Solution/HELP Required for: i want to pass an array to it or multiple values anyother way. so it would show multiple pre selected values.


Solution

  • You'll have to use a MultiSelectList instead of a SelectList. Something like

    @Html.DropDownList("pCategory[]", new MultiSelectList(new admin.Models.CategoryModel().getMultipleCategoryBySP(), "cat_id", "cat_name", --placeToProvideMultipleIntValue--), 
         new { @class = " form-control select2-multiple ", @data_toggle = "select2", @multiple = "multiple", @style = "width: 100%;" })