Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Alternatively Rendering EditorFor Based on User Role

Tags:

c#

asp.net-mvc

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

}
like image 492
Mehmet AVŞAR Avatar asked Sep 18 '13 13:09

Mehmet AVŞAR


People also ask

What is the difference between TextBoxFor and EditorFor in MVC?

it's absolutly wrong answer, becuase the key difference is that Texbox returns input and editorfor returns your template where input is default template for editorfor.

How do you use same view for add and edit in MVC?

Use the same View Model aka CreateUpdateViewModel and discarding extra UpdateBindingModel properties in the view but still posting the corresponding model on POST. Having your binding models as properties and select one or the other in the specific view. (better use @Html.

How do I add an EditorFor style?

EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use.


1 Answers

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!

like image 159
Fals Avatar answered Nov 15 '22 14:11

Fals