开发者

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) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜