从 OnActionExecuting 显式设置时 ViewModel 为 null
我研究了一些关于SO的问题,但似乎找不到答案。谁知道呢,也许我正在打破最佳实践。在阅读了关于使用布局注入器属性能够通过简单地指定属性来设置布局的优秀答案之后,我想我也可以使用这种类来对需要用户进行身份验证的属性控制器进行属性化。
因此,我不必在各处检查身份验证,而是编写了以下内容:
// class AdminAttribute
public class AdminAttribute : LayoutInjectorAttribute
{
// ctor
public AdminAttribute()
: base("_LayoutAdmin")
{
} // eo ctor
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
WebUser user = WebApplication.CurrentUser;
if (!user.IsAuthenticated)
{
filterContext.Result = new ViewResult() { ViewName = "Unauthorised" };
filterContext.Controller.ViewData.Model = new ViewModel(WebApplication.CurrentUser.Translate("msgunauthorisedtitle"));
filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
return;
}
base.OnActionExecuting(filterContext);
} // eo OnActionExecuting
} // eo class AdminAttribute
一切正常,除了处理视图时模型为 null
的事实之外。有谁知道为什么会这样?
查看页面(Unauthorized.cshtml)
@model Py2.Web.ViewModel
<h2>@Model.Title</h2>
提前致谢!
I researched a few questions on SO, but couldn't seem to locate the answer to this. Who knows, maybe I am breaking best-practice. After reading an excellent answer on using a layout injector attribute to be able to set the layout by simply specifying an attribute, I figured I could also use this kind of class to attribute controllers which require the user to be authenticated.
So, rather than me having to check authentication everywhere, I wrote the following:
// class AdminAttribute
public class AdminAttribute : LayoutInjectorAttribute
{
// ctor
public AdminAttribute()
: base("_LayoutAdmin")
{
} // eo ctor
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
WebUser user = WebApplication.CurrentUser;
if (!user.IsAuthenticated)
{
filterContext.Result = new ViewResult() { ViewName = "Unauthorised" };
filterContext.Controller.ViewData.Model = new ViewModel(WebApplication.CurrentUser.Translate("msgunauthorisedtitle"));
filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
return;
}
base.OnActionExecuting(filterContext);
} // eo OnActionExecuting
} // eo class AdminAttribute
All works, aside from the fact that the model is null
when processing the view. Does anyone have an idea why this may be?
The view page (Unauthorised.cshtml)
@model Py2.Web.ViewModel
<h2>@Model.Title</h2>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请像这样尝试:
请注意,无需显式调用
ExecuteResult
,这是 ASP.NET MVC 框架的工作,只需通过向 ViewResult 传递一个模型 (result.ViewData.模型=模型
)。另请注意此视图模型传递到视图结果的方式,而不是像您在代码中所做的那样传递到filterContext.Controller.ViewData.Model
。Try like this:
Notice that there is no need to call
ExecuteResult
explicitly, that's the job of the ASP.NET MVC frameowrk, simply return the ViewResult by passing it a model (result.ViewData.Model = model
). Also notice the way this view model is passed to the view result and not tofilterContext.Controller.ViewData.Model
as you were doing in your code.