【问题标题】:ASP.NET MVC Alternatively Rendering EditorFor Based on User RoleASP.NET MVC 或者基于用户角色呈现 EditorFor
【发布时间】:2013-09-18 13:51:47
【问题描述】:

我有一个包含当前用户属性的基本视图模型类,我需要 MVC 根据用户的管理员状态呈现文本框或标签。

目前,我正在这样做,但是代码必须重复很多次。

            @if (Model.CurrentUser.Admin)
            {
                @Html.EditorFor(m => m.Order.CustomerDiscount);
            }
            else
            {
                @Html.DisplayFor(m => m.Order.CustomerDiscount);
            }

是否可以创建自定义编辑器扩展?

            @Html.PrivilegedEditorFor(m=>m.Order.CustomerDiscount);

编辑:

感谢@Fals。这里有一个稍微不同的解决方案:

using System.Web.Mvc.Html;
public static class HtmlHelperExtensions
{
    public static MvcHtmlString PrivilegedEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool isAdmin)
    {
        if (isAdmin)
        {
            return htmlHelper.EditorFor(expression);
        } else {
            return htmlHelper.DisplayFor(expression);
        }
    }

}

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您可以为此创建一个自定义HTML Helper,例如:

    1) 将新的Class 添加到您的项目中,这将包含帮助程序。只需确保使用的模型包含CurrentUser.Admin

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Helpers;
    using System.Web.Mvc.Html;
    using System.Linq.Expressions;
    
    namespace MyAppName.Helpers
    {
        public static class HtmlPrivilegedHelper
        {
            public static MvcHtmlString PrivilegedEditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
            {
                // You can access the Model passed to the strongly typed view this way
                if (html.ViewData.Model.CurrentUser.Admin)
                {
                    return html.EditorFor(expression);
                }
    
                return html.DisplayFor(expression);
            }
        }
    }
    

    2)将命名空间添加到Views文件夹下的Web.config,这样就不用每次要使用时都包含命名空间了:

    <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="MyAppName.Helpers" /> //Here the helper reference
      </namespaces>
    </pages>
    </system.web.webPages.razor>
    

    希望对您有所帮助!

    【讨论】:

    • 嗯,谢谢你的表达细节。同时,我确实以更长的方式编写了这个帮助程序,并且无法像您那样使其更简单。首先,html.ViewData.Model.CurrentUser.Admin 部分无法确定,因为辅助方法只是将模型定义为一个简单的对象。所以我必须传递一个名为 isAdmin 的布尔参数,并且这个助手不必确定用户是否是管理员,查看模型。
    • 其次,htmlHelper.EditorFor 部分对我不起作用,我不得不分别使用System.Web.Mvc.Html.EditorExtensions.EditorFor(htmlHelper, expression)System.Web.Mvc.Html.DisplayExtensions.DisplayFor(htmlHelper, expression)
    【解决方案2】:

    尝试使用扩展方法。 可以使用您需要的方法创建静态类(不能是非静态或嵌套的),其中一个参数是 Html,并使用“this”关键字进行标记。 您可以在这里找到更多信息:http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

    【讨论】:

      【解决方案3】:

      您可以创建一个 HTML 辅助方法。一种方法是创建一个带有静态方法的新类,该方法返回一个 html 字符串以进行渲染。或者您可以扩展现有的 html 帮助程序类。

      这里有一个很棒的 asp.net 演练:http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

      【讨论】:

        猜你喜欢
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-04
        • 2014-10-14
        • 2014-10-22
        • 2013-08-01
        • 2011-12-07
        相关资源
        最近更新 更多