Render EditorTemplates based on authentication
Is it possible to render the editortemplates if a user is logged on and the displaytemplates as开发者_StackOverflow a default or do I need to create my own Html helper extension?
You need a custom helper method for this:
public static class HtmlExtensions
{
public static MvcHtmlString MyHelper<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression
)
{
if (htmlHelper.ViewContext.HttpContext.User.Identity.IsAuthenticated)
{
return htmlHelper.EditorFor(expression);
}
return htmlHelper.DisplayFor(expression);
}
}
And use:
<%: Html.MyHelper(x => x.SomeValue) %>
精彩评论