开发者

how do I get my selected list back with the selected item (asp.net mvc 2)

I try to make a view in ASP.NEt mvc 2 with a selectList.

I fill up the selectlist languages from my model (regDom)

listLangModel is a list of languages I retrieve from the database.

regDom.Languages = from l in listLangModel

               select new SelectListItem
                     {
                        Text = l.Name,
                        Value = l.ID
                     };

In my view I have this

        <div class="editor-label">
            <%: Html.LabelFor(model =>> model.Languages) %>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model =>  model.Languages, Model.Languages) %>
            <%: Html.ValidationMessageFor(model => model.Languages) %>
        开发者_运维百科</div>

When I run, the languages are in the dropdownlist on my page. But when I post it back to my server, the model doesn't contain the languages anymore. And I can't check which language is selected on the page.


The user can select only a single language from the drop down list, so you cannot expect it to populate the Languages property of your model which is a collection.

Add a property to hold the selected language in your model:

public string SelectedLanguage { get; set; }

and then bind the drop down list to this property:

<%: Html.DropDownListFor(model => model.SelectedLanguage, Model.Languages) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜