【问题标题】:ASP.NET MVC DropDownListFor with model of type List<string>具有 List<string> 类型模型的 ASP.NET MVC DropDownListFor
【发布时间】:2011-08-07 00:44:09
【问题描述】:

我有一个模型类型为List&lt;string&gt; 的视图,我想在页面上放置一个下拉列表,其中包含列表中的所有字符串作为下拉列表中的项目。我是 MVC 的新手,我该如何完成?

我试过了:

@model List<string>
@Html.DropDownListFor(x => x)

但这引发了错误。

【问题讨论】:

  • @Html.DropDownListFor(x => yourInputVariable,Model) 应该可以工作

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


【解决方案1】:

如果您在下拉列表中有想要的字符串类型列表,请执行以下操作:

编辑:澄清,使其成为一个更完整的例子。

public class ShipDirectory
{
    public string ShipDirectoryName { get; set; }
    public List<string> ShipNames { get; set; }
}

ShipDirectory myShipDirectory = new ShipDirectory()
{
    ShipDirectoryName = "Incomming Vessels",
    ShipNames = new List<string>(){"A", "A B"},
}

myShipDirectory.ShipNames.Add("Aunt Bessy");

@Html.DropDownListFor(x => x.ShipNames, new SelectList(Model.ShipNames), "Select a Ship...", new { @style = "width:500px" })

它给出了一个像这样的下拉列表:

<select id="ShipNames" name="ShipNames" style="width:500px">
    <option value="">Select a Ship...</option>
    <option>A</option>
    <option>A B</option>
    <option>Aunt Bessy</option>
</select>

获取控制器帖子上的值;如果您使用具有字符串列表作为属性的模型(例如 MyViewModel),因为您已指定 x => x.ShipNames,您只需将方法签名设置为(因为它将在模型中序列化/反序列化):

public ActionResult MyActionName(MyViewModel 模型)

像这样访问 ShipNames 值:model.ShipNames

如果您只想访问帖子上的下拉列表,则签名变为:

public ActionResult MyActionName(string ShipNames)

编辑:按照 cmets 已经阐明了如何访问模型集合参数中的 ShipNames 属性。

【讨论】:

  • 我不确定这是否有效。对于帖子 MyActionName(MyViewModel 模型),所选项目将绑定到什么属性。 ShipNames 属性仅包含可选值的列表。
  • 这很好用,而且比最高答案简单得多。 @RayLoveless 发布后,您的模型应包含类似“model.ShipNames = A”的属性/值。
【解决方案2】:

我知道很久以前有人问过这个问题,但我来这里是为了寻找答案,但对我能找到的任何东西都不满意。我终于在这里找到了答案:

https://www.tutorialsteacher.com/mvc/htmlhelper-dropdownlist-dropdownlistfor

要从表单中获取结果,请使用 FormCollection,然后通过模型名称提取每个单独的值:

yourRecord.FieldName = Request.Form["FieldNameInModel"];

据我所知,您为 FormCollection 提供的参数名称绝对没有区别 - 使用 Request.Form["NameFromModel"] 来检索它。

不,我没有深入了解这些魔法是如何在幕后发挥作用的。我只知道它有效...

我希望这可以帮助某人避免在我开始工作之前花费大量时间尝试不同的方法。

【讨论】:

    【解决方案3】:

    要制作下拉列表,您需要两个属性:

    1. 您将绑定到的属性(通常是整数或字符串类型的标量属性)
    2. 包含两个属性(一个用于值,一个用于文本)的项目列表

    在您的情况下,您只有一个字符串列表,无法利用它来创建可用的下拉列表。

    虽然对于数字 2,您可以让值和文本相同,但您需要一个属性来绑定。您可以使用弱类型版本的帮助器:

    @model List<string>
    @Html.DropDownList(
        "Foo", 
        new SelectList(
            Model.Select(x => new { Value = x, Text = x }),
            "Value",
            "Text"
        )
    )
    

    Foo 将是 ddl 的名称,并由默认模型绑定器使用。所以生成的标记可能看起来像这样:

    <select name="Foo" id="Foo">
        <option value="item 1">item 1</option>
        <option value="item 2">item 2</option>
        <option value="item 3">item 3</option>
        ...
    </select>
    

    据说一个更好的下拉列表视图模型如下:

    public class MyListModel
    {
        public string SelectedItemId { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    

    然后:

    @model MyListModel
    @Html.DropDownListFor(
        x => x.SelectedItemId,
        new SelectList(Model.Items, "Value", "Text")
    )
    

    如果您想预先选择此列表中的某个选项,您只需将此视图模型的SelectedItemId 属性设置为Items 集合中某个元素的相应Value

    【讨论】:

    • 这不起作用,你如何在控制器上获得值 Post... SelectedItemId 和 Items 都是空的。
    • 感谢您的帮助,只需要进行一些修改,因为我的方法是显示 ListsList 并具有 selectedValue,其余部分与 DropDownList HTML Helper 相同。
    • 在SelectedItemId属性中添加属性“BindProperty”,获取选中项的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多