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)
但这引发了一个错误。要创建下拉列表,您需要两个属性:
@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"];