填充 ASP.NET MVC DropDownList [英] Populating ASP.NET MVC DropDownList

查看:30
本文介绍了填充 ASP.NET MVC DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经在谷歌上搜索了几个小时并尝试了一切,但没有任何结果.我正在使用 Sharp Architecture 学习 MVC,并生成了一些用于创建 Client 对象的基本表单.我想用美国州的列表填充州下拉列表,并让用户从该列表中进行选择.我能够填充列表并取回值(以保存客户端)但是当我去编辑客户端时,客户端的当前状态未被选中.我已经在 SelectList 中设置了选定的值:

  • <label for="Client_StateProvince">StateProvince:</label><div><%= Html.DropDownListFor(c=>c.Client.StateProvince, new SelectList(Model.StateProvinces, "id", "Name", Model.Client.StateProvince), "-- 选择状态 --")%>
  • <%= Html.ValidationMessage("Client.StateProvince")%>

    这似乎还不够好.我错过了什么?

    解决方案

    <%= Html.DropDownListFor(c => c.Client.StateProvince.Id,新的 SelectList(Model.StateProvinces,ID",名称",模型.Client.StateProvince),"-- 选择状态--")%>

    这样做.

    希望这对其他人有帮助.

    ~李

    OK, I've been Googling for hours and trying everything and can't get anything to work. I am learning MVC using Sharp Architecture and have generated some basic forms for creating Client objects. I want to fill the state drop down list with a list of US states and let the user pick from that list. I am able to populate the list and get the value back (to save the client) but when I go to edit the client, the client's current state is not selected. I have set the selected value in the SelectList:

    <li>
        <label for="Client_StateProvince">StateProvince:</label>
        <div>
            <%= Html.DropDownListFor(c=>c.Client.StateProvince, new SelectList(Model.StateProvinces, "id", "Name", Model.Client.StateProvince), "-- Select State --")%>
        </div>
        <%= Html.ValidationMessage("Client.StateProvince")%>
    </li>
    

    This does not seem to be good enough. What am I missing?

    解决方案

    <%= Html.DropDownListFor(c => c.Client.StateProvince.Id, 
                                  new SelectList(Model.StateProvinces, 
                                                 "id", 
                                                 "Name", 
                                                 Model.Client.StateProvince), 
                                  "-- Select State --")%>
    

    This does it.

    Hope this helps someone else.

    ~Lee

    这篇关于填充 ASP.NET MVC DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆