views:

24

answers:

2

Hi all How can i preselect item in Html.DromDownListFor() ? i have code in view which inserts items to DropDownListFor

<div class="editor-field">
                <%var mesta = new List<SelectListItem>();
              SelectListItem aa = new SelectListItem();
              aa.Text = "---------VYBER MESTO---------";
              aa.Value = "0";
              mesta.Add(aa);
              foreach (var item in Model.MestoTbl)
              {
                  SelectListItem a = new SelectListItem();
                  a.Text = item.Mesto;
                  a.Value = item.MestoId.ToString();
                  mesta.Add(a);}%>
                <%: Html.DropDownListFor(model => model.Mesto.MestoId, mesta)%>
                <%: Html.ValidationMessageFor(model => model.Mesto.MestoId)%>
            </div>

this inserts 2 values MestoId & Mesto ....when i click on some database record (edit field) example =>

Name  Surname Mesto
--------------------
Peter Malik   Snina

Snina => Mestoid = 2 I wanna get .... if i click to edit record of Peter Malik the Html.DropDownListFor automatically preselect item Snina in list.

+1  A: 

You could use the SelectList constructor. See here.

fearofawhackplanet
i will try but i am not sure that i wil know how to use it
Peter M.
A: 

thanks now it is working.

<%: Html.DropDownListFor(model => model.Mesto.MestoId, new SelectList(mesta, "Value", "Text", Model.Ziak.MestoId))%>
Peter M.