【问题标题】:Dropdown list with limited range of choices in Asp.Net MVC?Asp.Net MVC 中选择范围有限的下拉列表?
【发布时间】:2011-02-21 21:35:32
【问题描述】:

我想将 View 中的字段绑定到 ViewModel 中的属性。该对象是“程序”并具有“名称”和“级别”属性。因此,用户应该能够设置程序的名称以及他/她对该程序的经验水平。

Program 是 Sql Server 数据库中的一个表,通过 Entity Framework 访问。所以将 ViewModel 中的 Level 属性绑定到文本框就很容易了:

@Html.TextBoxFor(model => model.Level)

但我想要一个级别数有限 (1-5) 的下拉列表。那么我该如何做到这一点并且仍然让下拉列表绑定到 ViewModel 属性呢?

【问题讨论】:

    标签: asp.net entity-framework asp.net-mvc-3 entity html.dropdownlistfor


    【解决方案1】:
    @Html.DropDownListFor( model => model.Level, new SelectList(new [] {1, 2, 3, 4, 5}) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多