如何创建一个HTML帮助像Html.BeginForm [英] How can I create a Html Helper like Html.BeginForm
问题描述
我有验证用户是否能够看到所述网页的一部分的基础上,一个角色的扩展方法
如果我简单的删除的内容,这给我带来了更多的工作,因为所有丢失的形式将不保存时正确注册,我要对付这种行为通过修改我的所有code,所以我想,为什么不只是使用显示:无;
属性
我想有这样的:
@using(Html.RoleAccess(的currentUser,RoleAccessType.Content_General_Website))
{
...
}
和这个会写类似:
< DIV CLASS =role_Content_General_Website的风格=显示:无;>
...
< / DIV>
或使用显示:块;
如果用户有访问...
我可以创建一个简单的的HtmlHelper
但我怎么写一个还输出结束< / DIV>
?
公共静态字符串RoleAccess(
这个的HtmlHelper帮手,
用户的UserInfo,
RoleAccessType角色)
{
返回
的String.Format(
< DIV CLASS ='ROLE_ {0}的风格=显示:{1}>中,
role.ToString(),user.HasAccess(角色));
}
公共静态类HtmlExtensions
{
私有类RoleContainer:IDisposable接口
{
私人只读的TextWriter _writer;
公共RoleContainer(TextWriter的作家)
{
_writer =作家;
} 公共无效的Dispose()
{
_writer.Write(< / DIV>中);
}
} 公共静态IDisposable的RoleAccess(此的HtmlHelper的HtmlHelper,串角色)
{
VAR用户= htmlHelper.ViewContext.HttpContext.User;
VAR风格=显示:无;;
如果(user.IsInRole(角色))
{
风格=显示:块;;
}
VAR作家= htmlHelper.ViewContext.Writer;
writer.WriteLine(< DIV CLASS = \\role_Content_General_Website \\的风格= \\+风格+\\>中);
返回新RoleContainer(作家);
}
}
,然后你可以使用它是这样的:
@using(Html.RoleAccess(管理员))
{
...
}
您可以明显适应助手的参数满足您的要求:
公共静态IDisposable的RoleAccess(
这个的HtmlHelper帮手,
用户的UserInfo,
RoleAccessType作用
)
{
VAR风格=显示:无;;
如果(user.HasAccess(角色))
{
风格=显示:块;;
}
VAR作家= htmlHelper.ViewContext.Writer;
writer.WriteLine(< DIV CLASS = \\ROLE_+ role.ToString()+\\的风格= \\+风格+\\>中);
返回新RoleContainer(作家);
}
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帮助像Html.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!