我在选择DropDownList的值时遇到问题。我一直在阅读所有类似的帖子,我无法获得解决方案。SelectListItem中的选定属性无法工作(DropDownListFor)
的实际做法似乎对我非常好,因为我可以检查,这将是选择列表的中的字段:
var selectList = new List<SelectListItem>(
from variable in someKindOfCollection
select new SelectListItem
{
Selected = variable.Property == selection,
Text = variable.Property,
Value = variable.Property
});
据称,这给了我完全控制。在建立selectList之后,我可以用调试器检查变量。一切正常,其中一个标记了“Selected”属性。
然后我用DropDownListFor为了显示对视图:
@Html.DropDownListFor(
g => g.SomePropertyInModel , selectList, new { @class = "cssClass" })
,但它不工作,从来没有......“渲染”下拉列表中,但没有被选中。
感谢很多:)
所有我要道歉的新的例子 第一。我一直在隐藏信息,当然是完全无意的。 所有的代码发生在一个剃刀循环内:
@foreach (var loopVariable in Model.Collection)
{
if (Model.SomeCondition != null)
{
selection = someValue;
}
var selectList = new List<SelectListItem>(
from variable in someKindOfCollection
select new SelectListItem
{
Selected = variable.Property == selection,
Text = variable.Property,
Value = variable.Property
});
@Html.DropDownListFor(
g => g.SomePropertyInModel , selectList, new { @class = "cssClass" })
}
所以,这是选择列表的的事实,导致出现该问题的局部变量?对不起,我没有想到它是。
您可以包含下拉菜单的HTML输出吗? – Brandon 2012-03-20 18:24:32
感谢布兰登,我已更新帖子。 – IoChaos 2012-03-21 14:06:33