【问题标题】:MVC - Show list of strings in DropDownListMVC - 在 DropDownList 中显示字符串列表
【发布时间】:2016-09-29 13:48:25
【问题描述】:

基于此代码(从 Visual Studio 自动生成的控制/视图代码):

<div class="form-group">
    @Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @*Comment: Want to replace EditorFor to DropDownList("Banana, "Apple", "Orange"*@
        @Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })*@
        @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
    </div>
</div>

我想添加类似的内容:

@Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })

(也是自动生成的代码,但用于UserId)而不是@html.EditorFor...

我能够通过控制器从 db 中看到 UserName(value = Id) 的列表:

ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", Test.UserId);

现在,我不希望 ViewBag 从数据库中读取,而是希望它列出 3 个不同的字符串,我将使用这些字符串让用户为 indata 选择(意思是,限制他们选择这 3 个字符串中的一个而不是写它)。

我希望它列出的字符串:

  • “香蕉”
  • “苹果”
  • “橙色”

我该怎么做?

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor


    【解决方案1】:

    试试这个,

    @Html.DropDownList("DropDown", new List<SelectListItem>
                                     { new SelectListItem { Text = "Banana", Value = "1", Selected=true},
                                       new SelectListItem { Text = "Apple", Value = "2"},
                                       new SelectListItem { Text = "Orange", Value = "3"}
                                       }, "Select Fruit")
    

    在模型中获取价值

    @Html.DropDownListFor(x => x.Id, new List<SelectListItem>
                                     { new SelectListItem { Text = "Banana", Value = "1", Selected=true},
                                       new SelectListItem { Text = "Apple", Value = "2"},
                                       new SelectListItem { Text = "Orange", Value = "3"}
                                       }, "Select Fruit")
    

    【讨论】:

    • 感谢它运行良好!只是一个问题,这仍然是工作区域的正确方法(例如平均安全)还是我应该考虑其他解决方案?
    • @Nyprez 是的,两者都是正确的方法。如果您想在模型中选择值而不是首先使用第二个选项。
    【解决方案2】:

    只需更改您分配给ViewBag.UserId f.e. 的内容。像这样:

    var fruits = new List<string> { "Banana", "Apple", "Orange" };
    ViewBag.Fruits = fruits.Select(f => new SelectListItem { Text = f, Value = f });
    

    【讨论】:

    • 在 View 中使用 @Html.DropDownList("Fruits", null, htmlAttributes: new { @class = "form-control" }) 给了我错误:There is no ViewData item of type 'IEnumerable&lt;SelectListItem&gt;' that has the key 'Fruits'.。我做错了什么?
    • 作为第二个参数,您必须像这样传递 ViewBag.Fruits:@Html.DropDownList("Fruits", ViewBag.Fruits, htmlAttributes: new { @class = "form-control" })
    【解决方案3】:

    这是您构建列表项的方法:

    ViewBag.Fruits = new SelectList(
        new List<SelectListItem>
        {
            new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
            new SelectListItem { Selected = false, Text = "Banana", Value = 0},
            new SelectListItem { Selected = false, Text = "Apple", Value = 1},
        }, "Value" , "Text", 1);
    

    【讨论】:

    • 试图在视图中调用它:@Html.DropDownList("Fruits", null, htmlAttributes: new { @class = "form-control" }) 但我收到错误:There is no ViewData item of type 'IEnumerable&lt;SelectListItem&gt;' that has the key 'Fruits'.
    【解决方案4】:

    这是使用枚举的另一种方法

    public enum Fruit { Banana, Apple, Orange }
    
    @Html.DropDownList("FruitSelection", 
                        new SelectList(Enum.GetValues(typeof(Fruit))),
                        "Select Fruit",
                        new { @class = "form-control" })
    

    【讨论】:

    • 我是否在控制器中添加public enum Fruit?如果这样做,我会收到错误“无法解析符号‘水果’”,其他 3 个也是如此。
    【解决方案5】:

    MVC 控制器

      ViewBag.PageSort = new List <SelectListItem> ()
             {
                 new SelectListItem () {Value = "1", Text = "Discounts"},
                 new SelectListItem () {Value = "2", Text = "New Items"},
                 new SelectListItem () {Value = "3", Text = "Lowest Price"},
                 new SelectListItem () {Value = "4", Text = "Highest Price"}
             };
    

    在视图中

    @Html.DropDownList("PageSort", ViewBag.PageSortas SelectList, "- Select Sort -", new { @id = "SortID", @name = "SortID", @class = "form-control" })
    

    【讨论】:

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