All viewmodels inherit from 'BaseViewModel'

2020-04-27 19:20发布

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 model = new SomeModel();

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

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

1条回答
Viruses.
2楼-- · 2020-04-27 19:36

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.

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

查看更多
登录 后发表回答