【问题标题】:Alternative way to populate selected value in HTML.Dropdownlistfor in MVC3.0在 MVC3.0 中填充 HTML.Dropdownlistfor 中选定值的替代方法
【发布时间】:2011-08-18 14:38:16
【问题描述】:

在下面的代码中,虽然 model.Title 保存了正确的选定值,但它没有在 ddl 中设置,id 为“Title”。

我可以用任何其他方式设置选定的值吗? (比如在文档中准备好了吗?)

    <td> 
@Html.DropDownListFor(model => model.Title, Model.TitleList, !Model.IsTitleEditable ? (object)new { id = "Title", @disabled = "disabled", @style = "width:250px;" } : (object)new { id = "Title", @style = "width:250px" })
</td>

在我的控制器中,选择列表填充如下:

model.TitleList = new SelectList(GetAllTitles(), "Code","Value"); 

在这种情况下,我使用的是其他重载方法,如何设置这个selectList的selectedValue属性?

【问题讨论】:

  • 能否展示设置model.TitleList的代码和设置model.Title的代码?

标签: jquery asp.net-mvc asp.net-mvc-3


【解决方案1】:

如果您将 model.TitleList 创建为 IEnumerable,其中您将 Text 设置为 SelectListItems 的值 并且 model.Value 是其中一个值SelectListItems 然后一切都应该工作。所以:

model.TitleList = GetAllTitles()
            .ToList()
            .Select(i => new SelectListItem { 
                                Value = i.Id.ToString(),
                                Text = i.Description });

model.Title = 5;

在你的视野中:

<td> 
@Html.DropDownListFor(model => model.Title, 
                      Model.TitleList, 
                      !Model.IsTitleEditable 
                         ? (object)new { @disabled = "disabled", @style = "width:250px;" } 
                         : (object)new { @style = "width:250px" })
</td>

请注意,HtmlAttributes 中的 id = "Title" 不是必需的,助手将为您创建一个 Id。

编辑 关于SelectListItem 上的Selected 属性存在一些混淆。这在使用DropDownListFor使用,它仅在DropDownList 中使用。所以对于DropDownListFor,您将模型的属性设置为您想要选择的值(上面提到的model.Title = 5;)。

【讨论】:

    【解决方案2】:

    如果Model.TitleListSelectList,您可以在模型创建期间填充SelectList 时指定所选值。举例说明:

    var model = new MyViewModel();
    var domainEntity = GetMyDomainEntity(id);
    
    // Create a selectlist using all your titles and specifying the Title value of the item
    // you're viewing as the selected item. See parameter options if you're not supplying
    // an object as the selected item value
    model.TitleList = new SelectList(GetAllTitles(), domainEntity.Title)
    

    那么你只需在你的视图中做你的Html.DropDownListFor

    【讨论】:

    • 但在我的控制器中,选择列表填充如下: model.TitleList = new SelectList(GetAllTitles(), "Code","Value");在这种情况下,当我使用其他重载方法时,如何设置此 selectList 的 selectedValue 属性?
    • 您可以将其填充为model.TitleList = new SelectList(GetAllTitles(), "Code", "Value", domainEntity.Title)。基本上添加第 4 个参数,即所选项目。
    【解决方案3】:

    我个人喜欢做的是有一个像这样的静态类:

    public static class SelectLists 
    {
        public static IList<SelectListItem> Titles(int selected = -1) 
        {
            return GetAllTitles()
                .Select(x => new SelectListItem  { 
                        Value = x.Id.ToString(), 
                        Text = x.Description, 
                        Selected = x.Id == selected
                    }).ToList();
        }
    }
    

    然后在我看来:

    @Html.DropDownListFor(x => x.Title, SelectLists.Titles(Model.Title), !Model.IsTitleEditable ? (object)new { id = "Title", @disabled = "disabled", @style = "width:250px;" } : (object)new { id = "Title", @style = "width:250px" });
    

    我所有的 SelectLists 都在那个类中,如果我有太多,我会在不同的类中分开。

    我觉得它很有用,因为如果您在另一个视图/操作中需要相同的下拉列表,您不必在控制器中重复代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多