0

我正在探索与登录用户角色相关的 Asp.net MVC 中的 DropDownBox 的通用解决方案。以下是场景:

如果 UserRole 为 X,则 pagesA 上的 DropDown 必须有多个值。

如果 UserRole 为 Y,则 pagesA 上的下拉列表必须具有单个值,并且下拉列表必须为只读,默认为单个值。

值将由控制器通过 ViewBag 提供给视图

有人可以帮我解决这种情况的扩展方法吗?

4

1 回答 1

0

是下载 MVC3 源代码的链接。您要查找的文件是 mvc3-rtm-sources\mvc3\src\SystemWebMvc\Mvc\Html\SelectExtensions.cs。该文件包含 DropDownList 和 DropDownList 的所有扩展方法。

您要查找的内容类似于以下内容:

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString DropDownListForRoles<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, UserRole role, IDictionary<string, object> htmlAttributes) {
    if (role == 'X') {
        //edit the select list as necessary
    }
    else if (role == 'Y') {
        // have the single value
        // add the read-only attribute to htmlAttributes
    }
    return DropDownListHelper(htmlHelper, expression, selectList, htmlAttributes);
}

这至少应该是您正在寻找的一个很好的起点。没有更多细节,我只能真正给你伪代码。但是,您应该能够从那里调整您需要的内容。

不要忘记检查它们对所有功能的不同覆盖。您可能认为现在不需要它们,但很高兴知道所有选项都在那里,以后应该会有所改变。像他们一样拥有它们,只是让这些方法返回您创建的方法,使用所有可能的参数调用该方法,并在必要时传入空值。

于 2013-01-18T20:36:22.097 回答