MVC5下拉默认值

时间:2014-07-25 17:23:05

标签: asp.net asp.net-mvc vb.net cookies

在我网站的每个页面上,我都有一个包含不同语言文化的下拉列表,我尝试使用用户Cookie中的当前字符串设置下拉列表的默认值。

如下所示,SelectList的默认参数为Request.Cookies("_culture").Value),但在每个请求中,下拉列表都会重置为索引0。

@Using Html.BeginForm("GetCulture", "Base", FormMethod.Post, New With { _
   .id = "CultureForm"
})
   @Html.DropDownList("Culture", _
   New SelectList(ViewBag.Culture, "Key", "Value", Request.Cookies("_culture").Value), _
   New With { _
      .class = "form-control lang-toggle hidden-xs", _
      .onchange = "document.getElementById('CultureForm').submit();"
   } _
)
End Using

有人能发现我做错了吗?有关于我应该知道的默认参数吗?我可以确认Request.Cookies("_culture").Value)中的值也在html select > option值范围内。

1 个答案:

答案 0 :(得分:0)

所以错误完全是浪费时间,对于那些遇到同样错误的人来说,这是错误的:

控件的名称与ViewBag中的键名称不同。

因此@Html.DropDownList("Culture" ...)ViewBag.Culture会导致冲突并导致默认设置未设置。

相关问题