Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC DropDownListFor,型号为List<;字符串>;_C#_Asp.net Mvc_Asp.net Mvc 3_Razor_Html Helper - Fatal编程技术网

C# ASP.NET MVC DropDownListFor,型号为List<;字符串>;

C# ASP.NET MVC DropDownListFor,型号为List<;字符串>;,c#,asp.net-mvc,asp.net-mvc-3,razor,html-helper,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,Html Helper,我有一个类型为List的视图,我想在页面上放置一个下拉列表,其中包含列表中的所有字符串作为下拉列表中的项目。我是MVC新手,我该如何做到这一点 我试过这个: @model List<string> @Html.DropDownListFor(x => x) @型号列表 @Html.DropDownListFor(x=>x) 但这引发了一个错误。要创建下拉列表,您需要两个属性: 要绑定到的属性(通常是integer或string类型的标量属性) 包含两个属性(一个用于值,一个

我有一个类型为
List
的视图,我想在页面上放置一个下拉列表,其中包含列表中的所有字符串作为下拉列表中的项目。我是MVC新手,我该如何做到这一点

我试过这个:

@model List<string>
@Html.DropDownListFor(x => x)
@型号列表
@Html.DropDownListFor(x=>x)

但这引发了一个错误。

要创建下拉列表,您需要两个属性:

  • 要绑定到的属性(通常是integer或string类型的标量属性)
  • 包含两个属性(一个用于值,一个用于文本)的项目列表
  • 在您的情况下,您只有一个字符串列表,无法利用该列表创建可用的下拉列表

    而2号。可以使值和文本与需要绑定到的属性相同。您可以使用弱类型的助手版本:

    @model List<string>
    @Html.DropDownList(
        "Foo", 
        new SelectList(
            Model.Select(x => new { Value = x, Text = x }),
            "Value",
            "Text"
        )
    )
    
    也就是说,下拉列表的更好视图模型如下所示:

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

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

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

    public class MyListModel
    {
        public string SelectedItemId { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    
    编辑:澄清,使其成为更完整的示例

    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" })
    
    公共类目录
    {
    公共字符串ShipDirectoryName{get;set;}
    公共列表船名{get;set;}
    }
    ShipDirectory myShipDirectory=新的ShipDirectory()
    {
    ShipDirectoryName=“输入船舶”,
    ShipNames=新列表(){“A”,“A B”},
    }
    myShipDirectory.ShipNames.Add(“阿姨贝西”);
    @Html.DropDownListFor(x=>x.ShipNames,新建SelectList(Model.ShipNames),“选择一艘船…”,新建{@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>
    
    
    选择一艘船。。。
    A.
    A B
    贝西姑妈
    
    获取控制器post上的值;如果您使用的模型(例如MyViewModel)具有字符串列表作为属性,因为您指定了x=>x.ShipNames,所以您只需将方法签名设置为(因为它将在模型中序列化/反序列化):

    公共操作结果MyActionName(MyViewModel模型)

    访问ShipNames值,如下所示:model.ShipNames

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

    公共操作结果MyActionName(字符串ShipName)


    编辑:根据评论澄清了如何访问model collection参数中的ShipNames属性。

    我意识到这个问题很久以前就被问到了,但我来这里寻找答案,对我能找到的任何东西都不满意。我终于在这里找到了答案:

    要从表单中获取结果,请使用FormCollection,然后按其模型名称提取每个值,如下所示:

    yourRecord.FieldName = Request.Form["FieldNameInModel"];
    
    据我所知,您给FormCollection指定的参数名称完全没有区别-使用Request.Form[“NameFromModel”]检索它

    不,我并没有深入地去看这些魔法是如何在被子里发挥作用的。我只知道它是有效的


    我希望这有助于有人避免我在尝试不同方法之前花费的时间。

    @Html.DropDownListFor(x=>yourInputVariable,Model)如果有效这不起作用,如何获得控制器上的值Post。。。SelectedItemId和Items均为空。感谢您的帮助,只需要执行一些修改,因为我的方法是显示一个
    列表的
    列表,并具有selectedValue,其余部分与
    DropDownList
    HTML帮助器相同。添加属性“BindProperty”选择EditEmid属性以获取所选项目的值。我不确定这是否有效。对于post MyActionName(MyViewModel模型),所选项目将绑定到哪个属性。ShipNames属性只包含可选值列表。这非常有效,比最高答案简单得多@发布后,模型应包含类似“model.ShipNames=a”的属性/值。
    yourRecord.FieldName = Request.Form["FieldNameInModel"];