ASP.NET MVC Alternatively Rendering EditorFor Based on User Role

前端 未结 3 533
小鲜肉
小鲜肉 2020-12-17 21:51

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\

相关标签:
3条回答
  • 2020-12-17 22:38

    You can create a HTML helper method. One way of doing this is by creating a new class with a static method that returns a string of html for rendering. Or you can extend the existing html helper class.

    There is a great asp.net walkthrough on it here: http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

    0 讨论(0)
  • 2020-12-17 22:41

    Try to use extension methods. It is possible to create static class (cannot be non-static or nested) with methods you need, which one parameter is Html and it is marked using 'this' keyword. More information you can find here: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

    0 讨论(0)
  • 2020-12-17 22:45

    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!

    0 讨论(0)
提交回复
热议问题