All viewmodels inherit from 'BaseViewModel', can I set this up in OnActionExecuting?
Asked Answered
B

1

1

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.

Bucolic answered 22/1, 2011 at 4:22 Comment(1)
possible duplicate of How to use a Base ViewModel in Asp.net MVC 2Ulda
R
2

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

Rightness answered 23/1, 2011 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.