views:

8

answers:

1

Is it possible to render the editortemplates if a user is logged on and the displaytemplates as a default or do I need to create my own Html helper extension?

A: 

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) %>
Darin Dimitrov
I assumed, thanks!
Marcus