【问题标题】:MVC3 DropDownListFor - a simple example?MVC3 DropDownListFor - 一个简单的例子?
【发布时间】:2011-08-22 03:47:55
【问题描述】:

我在我的 MVC3 应用程序中遇到了 DropDownListFor 问题。我能够使用 StackOverflow 来弄清楚如何让它们出现在视图上,但现在我不知道如何在提交视图模型时捕获其相应属性中的值。为了让它工作,我必须创建一个具有 ID 和 value 属性的内部类,然后我必须使用 IEnumerable<Contrib> 来满足 DropDownListFor 参数要求。但是,现在,MVC FW 应该如何将在此下拉列表中选择的值映射回我的视图模型上的简单字符串属性?

public class MyViewModelClass
{
    public class Contrib
    {
        public int ContribId { get; set; }
        public string Value { get; set; }
    }

    public IEnumerable<Contrib> ContribTypeOptions = 
        new List<Contrib>
        {
            new Contrib {ContribId = 0, Value = "Payroll Deduction"},
            new Contrib {ContribId = 1, Value = "Bill Me"}
        };

    [DisplayName("Contribution Type")]
    public string ContribType { get; set; }
}

在我的视图中,我将下拉列表放在页面上,如下所示:

<div class="editor-label">
    @Html.LabelFor(m => m.ContribType)
</div>
<div class="editor-field">
    @Html.DropDownListFor(m => m.ContribTypeOptions.First().ContribId, 
             new SelectList(Model.ContribTypeOptions, "ContribId", "Value"))
</div>

当我提交表单时,ContribType(当然)为空。

这样做的正确方法是什么?

【问题讨论】:

    标签: c# asp.net-mvc-3 html.dropdownlistfor


    【解决方案1】:

    你应该这样做:

    @Html.DropDownListFor(m => m.ContribType, 
                    new SelectList(Model.ContribTypeOptions, 
                                   "ContribId", "Value"))
    

    地点:

    m => m.ContribType
    

    是结果值所在的属性。

    【讨论】:

    • 谢谢!!!谢尔盖,这正是我多年来一直在寻找的。​​span>
    • 谢谢 :) 的回答有没有办法从这里显示“-- Select--”。 ?
    • @VeeKeyBee,您可以将新项目添加到列表contribTypeOptions,例如new Contrib {ContribId = -1, Value = "Please Select"},它将显示在下拉列表中。你可以检查ContribType 是否是-1 这意味着用户没有选择任何值
    • 你可以这样做:@Html.DropDownListFor(m =&gt; m.ContribType, new SelectList(Model.ContribTypeOptions, "ContribId", "Value", Model.ContribTypeOptions.First().ContribId), "Select, please")
    • @SergeyGavruk。这个答案在最近的几个问题上被用作欺骗,但由于语句 And the last param of Select list constructor is a selected value 使一些用户感到困惑 - 该方法忽略了最后一个参数(它在内部构建了自己的SelectList)。它的属性值 (ContribType) 决定了选择的内容(这就是模型绑定的工作方式),代码应该只是 @Html.DropDownListFor(m =&gt; m.ContribType, new SelectList(Model.ContribTypeOptions, "ContribId", "Value") 您能否编辑答案以更正。
    【解决方案2】:

    要在 DropDownList 中绑定动态数据,您可以执行以下操作:

    在 Controller 中创建 ViewBag,如​​下所示

    ViewBag.ContribTypeOptions = yourFunctionValue();
    

    现在在如下视图中使用这个值:

    @Html.DropDownListFor(m => m.ContribType, 
        new SelectList(@ViewBag.ContribTypeOptions, "ContribId", 
                       "Value", Model.ContribTypeOptions.First().ContribId), 
        "Select, please")
    

    【讨论】:

      【解决方案3】:

      我认为这会有所帮助: 在Controller中获取列表项和选定值

      public ActionResult Edit(int id)
      {
          ItemsStore item = itemStoreRepository.FindById(id);
          ViewBag.CategoryId = new SelectList(categoryRepository.Query().Get(), 
                                              "Id", "Name",item.CategoryId);
      
          // ViewBag to pass values to View and SelectList
          //(get list of items,valuefield,textfield,selectedValue)
      
          return View(item);
      }
      

      在视图中

      @Html.DropDownList("CategoryId",String.Empty)
      

      【讨论】:

      • 像这样将存储库暴露给视图通常不是一种上帝模式。
      【解决方案4】:
           @Html.DropDownListFor(m => m.SelectedValue,Your List,"ID","Values")
      

      这里的值是您要保存所选值的模型对象

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多