【问题标题】:Populating dropdown list in mvc 3在 mvc 3 中填充下拉列表
【发布时间】:2012-08-08 11:46:58
【问题描述】:

我有一张表包含

  • 国家名称和
  • 身份证

我想在下拉列表中填充 ddl 值作为 id 和文本作为名称。

我已经推荐了this,但没有成功

我是 MVC 的新手。我想知道我们如何在下拉列表中填充数据我在大多数解决方案中都尝试了谷歌,我在视图模型中得到了IEnumerable<SelectListItem>,但我无法在我的代码中得到它请给我这个解决方案或者是 der任何其他解决方案。

【问题讨论】:

    标签: asp.net-mvc-3 linq c#-4.0 entity-framework-4


    【解决方案1】:

    将 SelectList 与 ViewBag 一起使用: 例如:

    ViewBag.Countries = new SelectList(db.Countries.OrderBy(c => c.CountryName).ToList(), "CountryId", "CountryName", selectedValue != "" ? selectedValue : "AT");
    

    在视图中:

    @Html.DropDownListFor(model => model.Country, ViewBag.Countries as SelectList)
    @Html.ValidationMessageFor(model => model.Country)
    

    【讨论】:

      【解决方案2】:

      试试这个:

      @Html.DropDownListFor(x => x.Country, new SelectList(Model.DictionaryList, "Key", "Value"), "--Select Country--")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-02
        相关资源
        最近更新 更多