I’m designing some web app, I went for some _layout.cshtml, which fragment:
//some html
<h4>Your account</h4>
@{ Html.RenderPartial("UserMenu"); }
is responsible for rendering menu views. It has 3 possible states – logged in as admin, logged in as mere user, not logged. I made this partial view like this:
@if (User.IsInRole("Admin"))
{
@:Admin menu
}
else
{
if (User.Identity.IsAuthenticated)
{
@:Normal menu
}
else
{
@Html.ActionLink("Login", "Logon", "Account");
}
}
But I’m not satisfied with this solution as it has poor separation of logic and view. How do you suggest to improve it?
I don’t agree. For me this kind of logic is perfectly acceptable in the view. You could have written a custom HTML helper which could be used like this in your Layout:
and put the logic in the helper instead of using this partial. And if you decide to implement it here’s how it might look: