Asp.net mvc 在MVC中使用DropList或DropListFor忽略选定的特性
我有一个枚举,它是属性描述与数据库中索引的映射。我的ViewModel上有一个属性,表示该枚举的一个实例。我已尝试返回枚举实例列表,这意味着我要执行以下操作:Asp.net mvc 在MVC中使用DropList或DropListFor忽略选定的特性,asp.net-mvc,Asp.net Mvc,我有一个枚举,它是属性描述与数据库中索引的映射。我的ViewModel上有一个属性,表示该枚举的一个实例。我已尝试返回枚举实例列表,这意味着我要执行以下操作: @Html.DropDownListFor(m => m.CurrentFilter, Model.FilterTypes.Select(entry => new SelectListItem{ Text = entry.ToString(
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes.Select(entry =>
new SelectListItem{ Text = entry.ToString(), Value = ((int)entry).ToString()}),
new { @class = "normalcell", style = "WIDTH: 132px;" })
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes.Select(entry =>
new SelectListItem{ Text = entry.Text, Value = entry.Value, Selected = entry.Selected}),
new { @class = "normalcell", style = "WIDTH: 132px;" })
并返回SelectListItems列表,这意味着我要执行以下操作:
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes.Select(entry =>
new SelectListItem{ Text = entry.ToString(), Value = ((int)entry).ToString()}),
new { @class = "normalcell", style = "WIDTH: 132px;" })
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes.Select(entry =>
new SelectListItem{ Text = entry.Text, Value = entry.Value, Selected = entry.Selected}),
new { @class = "normalcell", style = "WIDTH: 132px;" })
在第二种情况下,当我调试时,我确定entry对象上的Selected属性对于正确的项是true。在这两种情况下,我的HTML中都没有写入“selected”属性,因此没有选择正确的项目。我还设置了一个断点,CurrentFilter确实具有正确的值,并且我的页面的其余部分也会相应地呈现,因此它正在查找该值
我已经写了很多有用的下拉列表,使用了类似的代码,我一辈子都不明白为什么不管我怎么做都不行
我也尝试过:
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes,
new { @class = "normalcell", style = "WIDTH: 132px;" })
在我看来,这是一种合乎逻辑的方法(返回SelectListItems的列表,只是在页面中不进行任何处理),但Selected属性仍然被忽略
更新:
我试着这样做:
@Html.DropDownList("CurrentFilter", Model.FilterTypes, new { @class = "normalcell", style = "WIDTH: 132px;" })
然后从请求中读取值。我返回的列表中只有一项选择了==true,而MVC忽略了它
这一点并不奇怪,但我很想知道为什么所有其他事情都不起作用
<select class="normalcell" id="CurrentFilter" name="CurrentFilter" style="WIDTH: 132px;">
@foreach (SelectListItem item in Model.FilterTypes)
{
if (item.Selected)
{
<option value="@item.Value" selected="selected">@item.Text</option>
}
else
{
<option value="@item.Value">@item.Text</option>
}
}
@foreach(在Model.FilterTypes中选择ListItem项)
{
如果(选定项)
{
@项目.案文
}
其他的
{
@项目.案文
}
}
我认为原因是MVC绑定引擎不知道如何处理枚举值。我认为您需要为视图模型创建一个“代理”属性。像这样的
public enum MyEnum { a, b, c, d };
public MyEnum EnumVal { get; private set; }
public string EnumProxy
{
get { return EnumVal.ToString(); }
set { EnumVal = (MyEnum)Enum.Parse(typeof(MyEnum), value); }
}
然后使用枚举名称构造下拉列表:
Type t = typeof(MyEnum);
var ddList = Enum.GetNames(t).Select(
item => new SelectListItem() { Text = item, Value = item }
).ToArray();
现在,您应该能够正常使用DropDownList:
@Html.DropDownListFor(model => model.EnumProxy, ddList)
我不确定是否有更整洁的解决方案。不过,这个应该可以使用。我的DDL教程演示了如何使用枚举。请参见我的教程和教程的第2部分解释:当字符串参数(要绑定的属性)和SelectList对象具有相同的名称时,将不使用选定的值
Darin在另一个常见的原因上发布了SO帖子,所选值未显示。请参见关于您的工作代码,您可以将其重写为
@(item.Selected?“Selected='Selected'”:)
谢谢-您是对的,但在我找到如何使用“适当的”MVC代码之前,这一直是一个占位符谢谢-我会尝试,我认为问题可能与枚举有关,但我希望,由于我的枚举有显式的int值,它会自动将它们作为int处理。