Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c# - 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));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...