【发布时间】:2012-02-12 13:47:59
【问题描述】:
我在使用 SelectExtension 的 DropDownList 方法时发现了一个奇怪的行为。我创建了一个 IEnumerable
public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable selectList);
我什至尝试过使用 SelectList 类并指定 SelectedValue 属性,但仍然没有选择正确的
关于如何保持所选值的任何想法?
写了 HACK 但更喜欢解决方案
以下是在使用从 MVC 发出的 Html 之前更改所选选项的代码破解。我不喜欢这个解决方案,但我不知道该怎么做。
// the following is a hack due to a precived MVC 3 bug
var html = SelectExtensions.DropDownList(helper, propertyName, source).ToHtmlString();
html = html.Replace("selected=\"selected\"", string.Empty);
html = html.Replace(string.Format("value=\"{0}\"", source.SelectedValue), string.Format("value=\"{0}\" selected=\"selected\"", source.SelectedValue));
【问题讨论】:
-
你是如何使用这个助手的?展示由您的模型结构、控制器和操作组成的完整示例,这将使我们对您的问题有更多的了解。目前你的问题对我来说毫无意义。
标签: asp.net-mvc-3 selectlistitem