基于用户角色的ASP.NET MVC或者渲染EditorFor [英] ASP.NET MVC Alternatively Rendering EditorFor Based on User Role

查看:82
本文介绍了基于用户角色的ASP.NET MVC或者渲染EditorFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基ViewModel类,其中包括当前用户的财产,我需要MVC呈现一个文本框或标签根据用户的管理状态。

目前,我这样做的方式,但在code有重演了不少。

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

是否有可能创建一个自定义编辑器扩展?

  @ Html.PrivilegedEditorFor(M = GT; m.Order.CustomerDiscount);

编辑:

感谢@Fals。稍微不同的解决方案是在这里:

 使用System.Web.Mvc.Html;
公共静态类HtmlHelperExtensions
{
    公共静态MvcHtmlString PrivilegedEditorFor<的TModel,TProperty>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,TProperty>>前pression,布尔isAdmin)
    {
        如果(isAdmin)
        {
            返回htmlHelper.EditorFor(如pression);
        }其他{
            返回htmlHelper.DisplayFor(如pression);
        }
    }}


解决方案

您可以创建自定义 HTML辅助此,对于为例:

1)新的添加到您的项目,这将包含帮手。只要确保所使用的模型包含 CurrentUser.Admin

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用System.Web.Helpers;
使用System.Web.Mvc.Html;
使用System.Linq.Ex pressions;命名空间MyAppName.Helpers
{
    公共静态类HtmlPrivilegedHelper
    {
        公共静态MvcHtmlString PrivilegedEditorFor<的TModel,TValue>(此的HtmlHelper<的TModel> HTML,防爆pression< Func键<的TModel,TValue>>前pression)
        {
            //您可以访问传递到强类型的视图这样的模式
            如果(html.ViewData.Model.CurrentUser.Admin)
            {
                返回html.EditorFor(如pression);
            }            返回html.DisplayFor(如pression);
        }
    }
}

2)添加命名空间到的Web.config 查看文件夹,然后你不必每次包括命名空间要使用它:

 < system.web.webPages.razor>
<主机factoryType =System.Web.Mvc.MvcWebRazorHostFactory,System.Web.Mvc,版本= 4.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
<页面pageBaseType =System.Web.Mvc.WebViewPage>
  <&命名空间GT;
    <添加命名空间=System.Web.Mvc/>
    <添加命名空间=System.Web.Mvc.Ajax/>
    <添加命名空间=System.Web.Mvc.Html/>
    <添加命名空间=System.Web.Optimization/>
    <添加命名空间=System.Web.Routing/>
    <添加命名空间=MyAppName.Helpers/> //这里的辅助基准
  < /命名空间>
< /页>
< /system.web.webPages.razor>

希望在您的帮助!

I have a base viewmodel class that includes a current user property, and I need MVC to render a textbox or label according to the user's admin status.

Currently, I'm doing this way, But the code has to repeat itself a lot.

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

Is it possible to create a custom editor extension?

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

Edit:

Thanks to @Fals. A slightly different solution is here:

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);
        }
    }

}

解决方案

You can create a custom HTML Helper for this, for exemple:

1) Add new Class to your project, this will contain the helper. Just make sure that the used model contains the 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) Add the namespace to the Web.config in the Views folder, then you don't have to include the namespace every time you want to use it:

<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>

Hopes this help you!

这篇关于基于用户角色的ASP.NET MVC或者渲染EditorFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆