所有的ViewModels从'BaseViewModel'继承,我可以在OnActionExecuting设置呢? [英] All viewmodels inherit from 'BaseViewModel', can I set this up in OnActionExecuting?

查看:1141
本文介绍了所有的ViewModels从'BaseViewModel'继承,我可以在OnActionExecuting设置呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我所有的行动,从BaseViewModel继承的典范,是有可能从OnActionExecuting方法初始化这个模式?

If all my actions have a model that inherits from BaseViewModel, is it possible to initialize this model from the OnActionExecuting method?

目前在我的所有动作我这样做:

Currently in all my actions I do this:

VAR模型=新SomeModel();

var model = new SomeModel();

model.User = Users.Get(...);

model.User = Users.Get(...);

现在我加载在OnActionExecuting用户对象,所以我希望我可以从那里不知怎么设置我的模型。

Now I am loading the user object in OnActionExecuting, so I was hoping I could setup my model from there somehow.

推荐答案

您不能做到这一点在OnActionExecuting,因为在这一点上你不知道用哪个BaseViewModel的子类。但是,您可以更新OnActionExecuted模型,其操作方法之后运行,但视图显示之前。

You can't do it in OnActionExecuting, since at that point you don't know which subclass of BaseViewModel to use. However, you can update the model in OnActionExecuted, which runs after the action method but before the view is rendered.

最有可能你正在填充这些属性只在视图中使用,但如果你需要在动作方法对它们的访问,你可以把它们放到ViewBag或OnActionExecuting控制器财产和他们OnActionExecuted添加到模型

Most likely you are populating these properties to use in the view only, but if you do need access to them in the action method you can put them into ViewBag or a controller property in OnActionExecuting and add them to the model in OnActionExecuted

这篇关于所有的ViewModels从'BaseViewModel'继承,我可以在OnActionExecuting设置呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆