OnActionExecuting 在开始操作之前添加到模型
我有以下内容:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
var model = filterContext.Controller.ViewData.Model as BaseViewModel;
if (model == null)
{
model = new BaseViewModel();
filterContext.Controller.ViewData.Model = model;
}
model.User = (UserPrincipal)filterContext.HttpContext.User;
model.Scheme = GetScheme();
}
现在逐步执行,我可以看到模型上的用户和方案正在填充。
当我采取行动时,它们都为空?
我在这里做错了什么?
除此之外,这是添加到模型的正确方法吗?
这是控制器代码:
[InjectStandardReportInputModel]
public ActionResult Header(BaseViewModel model)
{
//by this point model.Scheme is null!!
}
I have the following:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
var model = filterContext.Controller.ViewData.Model as BaseViewModel;
if (model == null)
{
model = new BaseViewModel();
filterContext.Controller.ViewData.Model = model;
}
model.User = (UserPrincipal)filterContext.HttpContext.User;
model.Scheme = GetScheme();
}
Now stepping through that i can see the user and scheme on the model are being populated.
By the time I get to the action however they are both null?
What am i doing wrong here?
And adding to this, is this the proper way of adding to the model?
Here is the controller code:
[InjectStandardReportInputModel]
public ActionResult Header(BaseViewModel model)
{
//by this point model.Scheme is null!!
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Controller.ViewData.Model
未填充 asp.net mvc 中的操作参数。该属性用于将数据从操作传递到视图。如果由于某种原因您不想使用自定义 Model Binder(这是在 asp.net-mvc 中填充操作参数的标准推荐方法),您可以 ActionExecutingContext.ActionParameters 属性
Controller.ViewData.Model
is not populating action parameters in asp.net mvc. That property is used to pass data from action to view.If for some reason you do not want to use custom Model Binder (which is the standard, reccommended way of populating action parameters in asp.net-mvc), you could you ActionExecutingContext.ActionParameters Property
回复有点晚了,但对其他人很有用。我们可以通过稍微装饰一下我们的属性来获取 OnActionExecuting 中 model 的值。
这是我们的过滤器类别
,仅此而已......如果您喜欢答案,请投票
Bit late for the reply but it will be useful for others. we can get the value of model in OnActionExecuting by just decoration our attribute a little more.
THIS IS OUR FILTER CLASS
AND THATS IT.....PLEASE VOTE IF YOU LIKE THE ANSWER