DropDownListFor不尊重的SelectList的Selected属性 [英] DropDownListFor not respecting Selected property of SelectList

查看:107
本文介绍了DropDownListFor不尊重的SelectList的Selected属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code以下行:

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

下拉正确建成,并选择确实是正确的,但MVC不会画下拉选择了正确的项目列表。标记不上的选项不输出选择的属性。

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. The markup does not output a selected attribute on an option.

的输出呈现为:

<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:

这只会影响GET,POST没有。想法?

This affects only GET, not POST. Ideas?

推荐答案

这往往难倒的人很多 - 显然路线图MVC 5包括更直观的DropDownList帮手

This tends to stump people a lot - apparently the roadmap for MVC 5 includes more intuitive DropDownList helpers.

当你这样做:

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

...的HTML助手将在其权力的的使用您的值设置为选择= TRUE 在<$做的一切C $ C>的SelectList 。它会:

... the HTML helper will do everything in its power not to use the value you set to Selected = true in your SelectList. It will:


  • 尝试从的ModelState 得到它 - 它不会对GET请求获得

  • 尝试从的ViewData 得到它 - 它的将会的搞定,因为你有一个 TimeOption 你的模型属性。

  • Try to get it from ModelState - which it won't get on the GET request
  • Try to get it from ViewData - which it will get, because you have a TimeOption property on your model.

只有当这两个尝试失败,将它屈从,并使用你问它用。

Only if both of those attempts fail, will it succumb, and use what you asked it to use.

所以,你的情况,它将使用 TimeOption 的当前值,您模型作为选择的值。这应该是罚款,因为这就是你想要在你的的SelectList 反正((INT)Model.TimeOption ),对吧?

So, in your case, it will use the current value of TimeOption on your model as the selected value. Which should be fine, because that's what you asked for in your SelectList anyway ((int)Model.TimeOption), right?

错误。因为它会的的投你的财产(枚举?),以 INT 像你一样。这将其转换为字符串。对于枚举能够产生枚举的名字 - 例如 PastMonth 。如果 TimeOption 是一个自定义类你们的,这将是的结果的ToString()

Wrong. Because it will not cast your property (enum?) to int like you do. It will convert it to a string. For an enum that yields the enumeration's name - e.g. PastMonth. If TimeOption is a custom class of yours, it will be the result of ToString().

然后,当它没有找到该字符串作为你的&中的一个;选择&GT; 值(因为它们都是整数),它决定不作出任何选中。

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.

在短: SelectListItem.Selected 对助手没有影响。如果你的助手调用指定一个名称(无论是从字面上或通过拉姆达前pression),助手将在的ModelState 使用任何的ViewData 使用该名称,并将其转换为字符串。

In short: SelectListItem.Selected has no effect on the helper. 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.

在大多数情况下,这种情况与枚举。绕过它最简单的方法是使用枚举的名称,而不是它的INT再presentation,至于 SelectListItem

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.

这篇关于DropDownListFor不尊重的SelectList的Selected属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆