2

I want to create Html helper that will display inner content only if user in role, Something like this:

@using(Html.AdminBlock()) {

}

And the code in the block will shown only if user in role...

How I can done it?

If you didn't understand what I mean here example, I want that those 2 codes will return equal result:

@if(Html.IsUserInRole("Admin")) {
    <span>hey</span>
} 

.

@using(Htm.RoleBlock()) {
    <span>hey</span>
}

2 Answers 2

1

What you are trying to do is not possible with an HTML helper that is returning IDisposable. The reason for that is because its body will always be rendered. You cannot conditionally exclude the body from being rendered in such a statement:

@using(Htm.RoleBlock()) {
    <span>hey</span>
}

Besides, the following looks readable enough:

@if(Html.IsUserInRole("Admin")) {
    <span>hey</span>
}

or you could write a helper that will return a boolean value and could be used like that:

@if(Html.IsAdmin()) {
    <span>hey</span>
}
Sign up to request clarification or add additional context in comments.

Comments

0

Update 2

So you want to keep as much logic out of the view as possible, in this case you can just pull the if conditions out of the view and into a HtmlHelper extension method.

public static class HtmlHelperExtensions
{
    public static bool IsAdmin(this HtmlHelper htmlHelper)
    {
        return htmlHelper.ViewContext.HttpContext.Current.User.IsInRole("admin");
    }
}

Usage:

@if (Html.IsAdmin()) {
    ...
}

Update

If what you want to only output something if a user is in a role this sort of helper is completely overkill. You should go for a simple if statement in your view.

@if (HttpContext.Current.User.IsInRole("admin")) {
    ...
}

Making a custom helper

I posted a blog post about this very topic last year where I opened up the ASP.NET MVC source to see how BeginForm() was put together and made my own. Here are the highlights, this will allow you to wrap a <div> around a block in an MVC view.

public class MvcDiv : IDisposable
{
    private bool _disposed;
    private readonly FormContext _originalFormContext;
    private readonly ViewContext _viewContext;
    private readonly TextWriter _writer;

    public MvcDiv(ViewContext viewContext)
    {
        if (viewContext == null)
        {
            throw new ArgumentNullException("viewContext");
        }

        _viewContext = viewContext;
        _writer = viewContext.Writer;
        _originalFormContext = viewContext.FormContext;
        viewContext.FormContext = new FormContext();

        Begin();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

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

    private void End()
    {
        _writer.Write("</div>");
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            _disposed = true;
            End();

            if (_viewContext != null)
            {
                _viewContext.OutputClientValidation();
                _viewContext.FormContext = _originalFormContext;
            }
        }
    }

    public void EndForm()
    {
        Dispose(true);
    }
}

Then put this extension method somewhere:

public static class HtmlHelperExtensions
{
    public static MvcDiv BeginDiv(this HtmlHelper htmlHelper)
    {
        return new MvcDiv(htmlHelper.ViewContext);
    }
}

Then you can use it like so:

@using (Html.BeginDiv())
{
    ...
}

8 Comments

But I won't to hide the div I want to simply not to render it.
Updated, you should just use @if (HttpContext.Current.User.IsInRole("admin"))
This what I don't want to do, I want that all the Logic (if) will outside the view...
Updated again with another solution.
You don't understand I want to remove all If sentences out of View
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.