I’m new to ASP.NET MVC and I have a partial view with a combo box, which I need to dynamically populate.
My understanding is that I can put the list data in the ViewBag for the view, and it will be available from my partial view.
However, I want to be able to use the partial view from several different pages. Must all pages that use the partial view populate ViewBag with the list data? Is there any way to encapsulate this within my partial view?
Look at this question: ViewModel Best Practices. May be you should create a base view model containing the list data and (if needed) inherit other view models from it.
For instance:
You can inherit other view models from ViewModelWithFooList if you need to.
In controller:
And in your views:
Views and partial view “FooList” must be strongly typed.