0

Using :

How to create a Dynamic Bootstrap Multiselect

It was pretty easy to come up with this :

        <fieldset class="my-fieldset" style="width: 500px;">
            <legend class="my-fieldset">Catégories</legend>
            <div style="text-align: center;">
                <div style="display: inline;">
                    <span><b>Matériels</b></span>
                    <select style="display: none;" class="chkveg" name="categories" multiple="multiple">
                        @foreach (var configCateg in listeCategories)
                        {
                            if (configCateg.APPR_CATEGORIE.CATEGORIE.StartsWith("IF"))
                            {
                                <option selected="False" value="@configCateg.DESCRIPTION">@configCateg.DESCRIPTION</option>
                            }
                        }
                    </select>
                 </div>
            </div>
        </fieldset>

But what if i want to have some of the options selected and others not...

I know there is a selected property but it didn't change anything when i tried selected="false"

Any help is apreciated

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • 1
    if you dont want an option selected you have to remove the selected attributue.. it does not work on a true/false basis – JamieD77 Aug 08 '16 at 20:30

1 Answers1

1

Unfortunately all the below will result in a selected option item.

<option selected >One</option>
<option selected = "False" >One</option> 
<option selected = "0" >One</option> 
<option selected value="1">One</option>
<option selected=false value="2">222</option>
<option selected="i really dont want this" value="3">33</option>

If you do not want an item selected, you should render an option item without the selected attribute.

<option value="1" selected >One</option>
<option value="2">This one will not be selected</option>
<option  value="3"  selected >One</option>

You might consider using the Html.DropdDownListFor helper method or SELECT tag helper to render the select element with some options selected.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • So it's not possible to do it with what i've done, i have to do it another way ? – Antoine Pelletier Aug 09 '16 at 13:40
  • selected="false" will also make the option selected. I suggest you use the DropDownListFor helper method, which does this for you http://stackoverflow.com/questions/36919078/i-cannot-set-default-value-on-dropdownlistfor/36919952#36919952 – Shyju Aug 09 '16 at 13:46
  • Ok, bah... i'm just gonna start over and do something totaly custom on my own as usual, at least this way i will be able to do anything i want and make it work like i wish. Thanks. – Antoine Pelletier Aug 09 '16 at 15:22