1
我將數據提交給IAuthorizationFilter.OnAuthorization過程中的ViewBag,並將其放入_Layout.cshtml-in,但是當我在普通視圖中使用分部視圖時,ViewBag中的數據爲空。 這是正常行爲,還是我做錯了什麼?ViewBag missing
public class MyAuthorizeAttribute : AuthorizeAttribute, IAuthorizationFilter
{
void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
filterContext.Controller.ViewBag.Name = this.name;
filterContext.Controller.ViewBag.Menus = user.GetMenu(this.role);
}
...
}
的_Layout.cshtml:
<section id="login">
Hello, <span class="username">@ViewBag.Name</span>!
</section>
<nav>
@Html.MenuLink(ViewBag.Menus as List<Entity.Models.MENU>, (string)ViewBag.Name)
</nav>
的觀點:
@model Web.ViewModels.RegistrationEntryViewModel
@Html.Partial("_RegistrationEntry", Model)
行動:
[MyAuthorize("ADMINISTRATORS")]
[HttpGet]
public ActionResult NewRegistrationEntry()
{
//The viewbag is already null here
...
}
如果有一些代碼說明它會更容易理解問題 – Andrei