我如何创建一个 Html Helper 像 Html.BeginForm [英] How can I create a Html Helper like Html.BeginForm

查看:25
本文介绍了我如何创建一个 Html Helper 像 Html.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展方法,可以根据角色验证用户是否能够看到网页的一部分.

如果我简单地删除内容,这会给我带来更多的工作,因为所有丢失的表单在保存时都不会被正确注册,我必须通过修改我的所有代码来处理这种行为,所以我想为什么不直接使用 display:none; 属性?

我想要类似的东西:

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

这会写成这样:

或者使用 display:block; 如果用户有访问权限...

我可以创建一个简单的 HtmlHelper 但我如何编写一个也输出结尾的 </div>?

public static string RoleAccess(这个 HtmlHelper 助手,用户信息用户,RoleAccessType 角色){返回字符串.格式("<div class='role_{0}' style='display:{1}'>",role.ToString(), user.HasAccess(role));}

解决方案

public static class HtmlExtensions{私有类 RoleContainer : IDisposable{私有只读 TextWriter _writer;公共角色容器(TextWriter 作家){_writer = 作家;}公共无效处置(){_writer.Write("</div>");}}public static IDisposable RoleAccess(this HtmlHelper htmlHelper, string role){var user = htmlHelper.ViewContext.HttpContext.User;var style = "display:none;";如果 (user.IsInRole(role)){样式=显示:块;";}var writer = htmlHelper.ViewContext.Writer;writer.WriteLine("<div class="role_Content_General_Website" style="" + style + "">");返回新的 RoleContainer(writer);}}

然后你可以像这样使用它:

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

您显然可以调整助手的参数以满足您的要求:

public static IDisposable RoleAccess(这个 HtmlHelper 助手,用户信息用户,角色访问类型角色){var style = "display:none;";if (user.HasAccess(role)){样式=显示:块;";}var writer = htmlHelper.ViewContext.Writer;writer.WriteLine("<div class="role_" + role.ToString() + "" style="" + style + "">");返回新的 RoleContainer(writer);}

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

解决方案

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

这篇关于我如何创建一个 Html Helper 像 Html.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆