简体   繁体   English

DropDownList不尊重SelectList的Selected属性

[英]DropDownListFor not respecting Selected property of SelectList

I have the following line of code: 我有以下代码行:

@Html.DropDownListFor(x => x.TimeOption, new SelectList(Model.TimeOptions, "Value", "Name", (int)Model.TimeOption))

The drop down is properly built, and Selected is indeed correct but MVC will not draw the drop down list with the correct item selected. 正确构建了下拉列表,并且选择确实是正确的但MVC不会在选择了正确的项目的情况下绘制下拉列表。 The markup does not output a selected attribute on an option. 标记不会在选项上输出选定的属性。

The output is rendered as: 输出呈现为:

<option value="0">Past Day</option>
<option value="1">Past Week</option>
<option value="2">Past Month</option>
<option value="3">Past Year</option>
<option value="4">Start of Time</option>

Yet if you look at the attached screenshot you can see it is correctly selected: 然而,如果您查看附加的屏幕截图,您可以看到它已正确选择:

观察清单

This affects only GET, not POST. 这仅影响GET,而不影响POST。 Ideas? 想法?

This tends to stump people a lot - apparently the roadmap for MVC 5 includes more intuitive DropDownList helpers. 这往往会让人们陷入困境 - 显然MVC 5的路线图包含更直观的DropDownList助手。

When you do: 当你这样做时:

@Html.DropDownListFor(x => x.TimeOption ...)

... the HTML helper will do everything in its power not to use the value you set to Selected = true in your SelectList . ... HTML帮助程序将尽其所能, 使用您在SelectList设置为Selected = true的值。 It will: 它会:

  • Try to get it from ModelState - which it won't get on the GET request 尝试从ModelState获取它 - 它不会得到GET请求
  • Try to get it from ViewData - which it will get, because you have a TimeOption property on your model. 尝试从ViewData获取它 - 它将获得它,因为您的模型上有一个TimeOption属性。

Only if both of those attempts fail, will it succumb, and use what you asked it to use. 只有当这两个尝试都失败时,它才会屈服,并使用你要求它使用的东西。

So, in your case, it will use the current value of TimeOption on your model as the selected value. 因此,在您的情况下,它将使用模型上的TimeOption的当前值作为选定值。 Which should be fine, because that's what you asked for in your SelectList anyway ( (int)Model.TimeOption ), right? 哪个应该没问题,因为那是你在SelectList要求的( (int)Model.TimeOption ),对吧?

Wrong. 错误。 Because it will not cast your property ( enum ?) to int like you do. 因为它不会像你那样将你的属性( enum ?)转换为int It will convert it to a string. 它会将其转换为字符串。 For an enum that yields the enumeration's name - eg PastMonth . 对于一个enum能产生枚举的名字-比如PastMonth If TimeOption is a custom class of yours, it will be the result of ToString() . 如果TimeOption是您的自定义类,则它将是ToString()的结果。

Then, when it doesn't find that string as one of your <select> values (because they're all integers), it decides not to make anything selected. 然后,当它没有找到该字符串作为您的<select>值之一时(因为它们都是整数),它决定不进行任何选择。

In short: SelectListItem.Selected has no effect on the helper. 简而言之: SelectListItem.Selected对帮助程序没有影响。 If your helper call specifies a name (either literally or through a lambda expression), the helper will use anything in ModelState or ViewData with that name, and convert it to a string. 如果您的帮助程序调用指定了一个名称(通过字面或通过lambda表达式),则帮助程序将使用ModelStateViewData具有该名称的任何内容,并将其转换为字符串。

In most cases this happens with enumerations. 在大多数情况下,这会发生在枚举中。 The easiest way to get around it is to use the enumeration's name, rather than its int representation, as the value for the SelectListItem . 解决它的最简单方法是使用枚举的名称而不是其int表示作为SelectListItem的值。

You've already got a good explanation above as to why its not working, but just to add my 2p 你已经在上面得到了一个很好的解释,为什么它不起作用,但只是添加我的2p

Generally I prefer using an enumerable of SelectListItems as the source of dropdownitems, although either should work. 一般来说,我更喜欢使用可枚举的SelectListItems作为dropdownitems的来源,尽管两者都应该有用。 It's just preferable doing it this way as there's no need to specified which item is selected - the MVC helper will infer it from the one who's Value matches the TimeOption.ToString() method. 这样做更可取,因为不需要指定选择哪个项 - MVC帮助程序将从Value与TimeOption.ToString()方法匹配的那个推断出它。

ie

@Html.DropDownListFor(x => x.TimeOption, ((IEnumerable<TimeOption>)Model.TimeOptions).ToList().ConvertAll(y => new SelectListItem(){Text = y.Name, Value = y.Value.ToString()})

NB - I'm not sure what type Model.TimeOptions is here - if its not IEnumerable< TimeOption> then replace it. 注意 - 我不确定Model.TimeOptions在这里是什么类型 - 如果它不是IEnumerable <TimeOption>然后替换它。 (if its already a list then .ToList() is not necessary) (如果它已经是一个列表,则.ToList()不是必需的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SelectListItems的SelectList的DropDownListFor中未选择所选值 - Selected Value not selected in a DropDownListFor of SelectList of SelectListItems DropDownList用于在使用共享的SelectList时设置其他属性的值 - DropDownListFor setting value of other property when using shared SelectList SelectList构造函数未设置SelectListItem上的选定属性 - Selected property on SelectListItem not being set by SelectList constructor 带有日期时间的ASP.NET MVC 5 Html.DropDownListFor不起作用选择了SelectList - ASP.NET MVC 5 Html.DropDownListFor with datetime doesn't work SelectList selected 在视图中呈现时,魔术贴选择了模板ViewModel的SelectList属性 - Template ViewModel's SelectList property magically selected when rendered in view MVC DropDownListFor所选项目由于属性名称而未绑定 - MVC DropDownListFor selected item not binding due to property name 带有null selectList参数的Html.DropDownListFor - Html.DropDownListFor with null selectList parameter 使用默认值从 SelectList 创建 DropDownListFor - Create DropDownListFor from SelectList with default value 手动将 SelectListItem 添加到 SelectList 以在 DropDownListFor 中使用 - Adding SelectListItem manually to SelectList to use in DropDownListFor 设置选择列表中的选定项目 - Set selected item in selectlist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM