I have problem with dropdown list for my model. I cant set default selected value. Here is my code. Select list definition:

var productGroups = db.sl_GrupaKh.AsNoTracking()
        .Where(g => g.kh__Kontrahent.Any())
        .Select(n => new {n.grk_Id, n.grk_Nazwa })
        .OrderBy(n => n.grk_Nazwa).ToList();
        productGroups.Add(new { grk_Id = 0, grk_Nazwa = "(" + Resources.Resources.NoneLabel + ")" });
        productGroups.Add(new { grk_Id = -1, grk_Nazwa = "(" + Resources.Resources.AnyLabel + ")" });

        var selectedItem = productGroups.SingleOrDefault(x=>x.grk_Id==selected);
        SelectList selectList = new SelectList(productGroups,"grk_Id","grk_Nazwa",selected);

grk_Id - int type, grk_Nazwa-string type, selected - int type I have watched that selected matches one of options value.

Now view code:

    @Html.DropDownListFor(m => m.customerMenu, Model.customerMenu,
new  {@class="myClass" })

And output:

<select class="dasdas" id="customerMenu" name="customerMenu">
<option value="2">Drogerie</option>
<option value="3">Fabryki</option>
<option value="4">Hurtownie</option>
<option value="5">Importerzy</option>
<option value="1">Podstawowa</option>
<option value="6">Sklepy</option>
<option value="0">(brak)</option>
<option value="-1">(dowolna)</option>
</select>

Im looking forward for any help. Thank You :)

Related posts

Recent Viewed