asp.net-mvc – SelectListItem中的Selected属性永远不会起作用(DropDownListFor)

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – SelectListItem中的Selected属性永远不会起作用(DropDownListFor)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在选择DropDownList的值时遇到问题.我一直在阅读所有类似的帖子,我无法得到解决方案.

实际的方法对我来说似乎非常好,因为我可以检查SelectList中的字段:

  1. var selectList = new List<SelectListItem>(
  2. from variable in someKindOfCollection
  3. select new SelectListItem
  4. {
  5. Selected = variable.Property == selection,Text = variable.Property,Value = variable.Property
  6. });

据说,这给了我完全的控制权.在构建了selectList之后,我可以使用调试器检查变量.一切都很好,其中一个标有“Selected”属性.

然后我使用DropDownListFor来显示视图:

  1. @Html.DropDownListFor(
  2. g => g.SomePropertyInModel,selectList,new { @class = "cssClass" })

但它没有用,从来没有…“渲染”下拉列表,但没有选择任何东西.

非常感谢 :)

新例子
首先,我想道歉.我一直在隐藏信息,当然是完全无意的.
所有代码都发生在Razor For循环中:

  1. @foreach (var loopVariable in Model.Collection)
  2. {
  3. if (Model.SomeCondition != null)
  4. {
  5. selection = someValue;
  6. }
  7.  
  8. var selectList = new List<SelectListItem>(
  9. from variable in someKindOfCollection
  10. select new SelectListItem
  11. {
  12. Selected = variable.Property == selection,Value = variable.Property
  13. });
  14.  
  15. @Html.DropDownListFor(
  16. g => g.SomePropertyInModel,new { @class = "cssClass" })
  17.  
  18. }

那么,selectList是导致行为的局部变量的事实?对不起,我没想到就是这样.

解决方法

我认为你遇到了同样的问题.我查看了源代码以找到我的解决方案,看起来这对我来说是一个错误.下面的DropDownListFor应该有帮助,关键是你将选中的值传递给html帮助器.希望这可以帮助.
  1. public static class SelectExtensions {
  2. public static IHtmlString DropDownListFor<TModel,TProperty>(this HtmlHelper<TModel> helper,Expression<Func<TModel,TProperty>> expression,IEnumerable<SelectListItem> selectList,string selectedValue,string optionLabel,object htmlAttributes = null) {
  3. return DropDownListHelper(helper,ExpressionHelper.GetExpressionText(expression),selectedValue,optionLabel,htmlAttributes);
  4. }
  5.  
  6. /// <summary>
  7. /// This is almost identical to the one in ASP.NET MVC 3 however it removes the default values stuff so that the Selected property of the SelectListItem class actually works
  8. /// </summary>
  9. private static IHtmlString DropDownListHelper(HtmlHelper helper,string name,object htmlAttributes) {
  10. name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
  11.  
  12. // Convert each ListItem to an option tag
  13. var listItemBuilder = new StringBuilder();
  14.  
  15. // Make optionLabel the first item that gets rendered
  16. if (optionLabel != null)
  17. listItemBuilder.AppendLine(ListItemToOption(new SelectListItem() { Text = optionLabel,Value = String.Empty,Selected = false },selectedValue));
  18.  
  19. // Add the other options
  20. foreach (var item in selectList) {
  21. listItemBuilder.AppendLine(ListItemToOption(item,selectedValue));
  22. }
  23.  
  24. // Now add the select tag
  25. var tag = new TagBuilder("select") { InnerHtml = listItemBuilder.ToString() };
  26. tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
  27. tag.MergeAttribute("name",name,true);
  28. tag.GenerateId(name);
  29.  
  30. // If there are any errors for a named field,we add the css attribute
  31. ModelState modelState;
  32.  
  33. if (helper.ViewData.ModelState.TryGetValue(name,out modelState)) {
  34. if (modelState.Errors.Count > 0)
  35. tag.AddCssClass(HtmlHelper.ValidationInputCssClassName);
  36. }
  37.  
  38. // Add the unobtrusive validation attributes
  39. tag.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(name));
  40.  
  41. return tag.ToHtmlString(TagRenderMode.Normal);
  42. }
  43.  
  44. private static string ListItemToOption(SelectListItem item,string selectedValue) {
  45. var tag = new TagBuilder("option") { InnerHtml = HttpUtility.HtmlEncode(item.Text) };
  46.  
  47. if (item.Value != null)
  48. tag.Attributes["value"] = item.Value;
  49.  
  50. if ((!string.IsNullOrEmpty(selectedValue) && item.Value == selectedValue) || item.Selected)
  51. tag.Attributes["selected"] = "selected";
  52.  
  53. return tag.ToString(TagRenderMode.Normal);
  54. }
  55. }

猜你在找的asp.Net相关文章