Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a Html Helper like Html.BeginForm

I have an Extension Method that verifies if the user is able to see a portion of the webpage, based on a Role.

If I simple remove the content, this brings me more work as all the missing forms will not be correctly registered upon save and I have to deal with this behavior by modifying all my code, so I thought why not just use display:none; attribute?

I would like to have something like:

@using(Html.RoleAccess(currentUser, RoleAccessType.Content_General_Website))
{
    ...
}

and that this would write something like:

<div class="role_Content_General_Website" style="display:none;">
    ...
</div>

or use display:block; if the user has access...

I can create a simple HtmlHelper but how do I write one that also outputs the ending </div>?

public static string RoleAccess(
         this HtmlHelper helper, 
         UserInfo user, 
         RoleAccessType role)
{
   return 
       String.Format(
            "<div class='role_{0}' style='display:{1}'>", 
            role.ToString(), user.HasAccess(role));
}
like image 603
balexandre Avatar asked Nov 28 '11 07:11

balexandre


People also ask

How do you create an HTML helper?

The easiest way to create a new HTML Helper is to create a static method that returns a string. Imagine, for example, that you decide to create a new HTML Helper that renders an HTML <label> tag. You can use the class in Listing 2 to render a <label> .

Can we create custom HTML helper?

We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

What is HTML BeginForm ()?

BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html. BeginForm extension method in ASP.NET MVC5. BeginForm("ActionMethod", "ControllerName","Get⁄Post Method")


1 Answers

public static class HtmlExtensions
{
    private class RoleContainer : IDisposable
    {
        private readonly TextWriter _writer;
        public RoleContainer(TextWriter writer)
        {
            _writer = writer;
        }

        public void Dispose()
        {
            _writer.Write("</div>");
        }
    }

    public static IDisposable RoleAccess(this HtmlHelper htmlHelper, string role)
    {
        var user = htmlHelper.ViewContext.HttpContext.User;
        var style = "display:none;";
        if (user.IsInRole(role))
        {
            style = "display:block;";
        }
        var writer = htmlHelper.ViewContext.Writer;
        writer.WriteLine("<div class=\"role_Content_General_Website\" style=\"" + style + "\">");
        return new RoleContainer(writer);
    }
}

and then you can use it like this:

@using(Html.RoleAccess("Administrator"))
{
    ...
}

You could obviously adapt the arguments of the helper to match your requirements:

public static IDisposable RoleAccess(
    this HtmlHelper helper, 
    UserInfo user, 
    RoleAccessType role
)
{
    var style = "display:none;";
    if (user.HasAccess(role))
    {
        style = "display:block;";
    }
    var writer = htmlHelper.ViewContext.Writer;
    writer.WriteLine("<div class=\"role_" + role.ToString() + "\" style=\"" + style + "\">");
    return new RoleContainer(writer);
}
like image 75
Darin Dimitrov Avatar answered Oct 15 '22 18:10

Darin Dimitrov