My View uses @Html.Action(…)
to reflect various functional blocks. In opening the page, the site shows Authorization dialog box for users that do not have role pointed in controller method. (e.g. “manager” or “caller”) On pressing “cancel” get:
401 - Unauthorized: Access is denied due to invalid credentials.
May I achieve changes of my application behavior in case a user has no required roles, @Html.Action is ignored or nothing is shown?
My VIew:
@model InApp.ViewModel.ListViewModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="CallList">
@Html.Action("List","Call",new {id=Model.id})
</div>
<div class="Order">
@Html.Action("Create","Order",new {id=Model.id})
</div>
Controllers:
[Authorize(Roles = "manager, caller")] //if a user is not 'manager' or 'caller'
public PartialViewResult List() // nothing is shown
{
//...private
return PartialView();
}
[Authorize(Roles = "manager, admin")]
public PartialViewResultCreate()
{
//...private
return PartialView();
}
Trying to find the correct solution I have found similar questions:
Ignore @Html.Action() if user not in Role and asp.net MVC3 razor: display actionlink based on user role But I do not like “if” condition in my View. I am looking for a complex solution to hide and show separate parts using only AuthorizeAttribute and to avoid if – else in View or Controller. Thanks for any help!