ASP.NET MVC Model vs ViewData for select lists

I have an ASP.NET MVC application with a lot of dropdowns and multiple choice lists. In fact, many lists of options.

My question is: is it better to pass these lists to the view as part of the Model or as ViewData?

I am currently passing them as ViewData, since they really don't need them in the model, and they seem potentially cumbersome to go through the model (I get the selected item or items, which is really all I need). ViewData, on the other hand, requires casting in a view, which is not as good as a strongly typed model.

Is there any best practice here? Even the ideas of the pros and cons for any of them would be appreciated.

+6
asp.net-mvc model multi-select
source share
2 answers

I recommend that you use ViewModels to transfer this data. This is a bug that tends to use ViewData with magic strings, and I prefer to use intellisense instead of trying to remember magic strings. And you do not need to create these selection lists in the controller. Just use IEnumerable and use the ToSelectList extension method from MvcContrib in the view.

+6
source share

I tend to use ViewData if I only have one item that I pass to the view. So, if you send multiple objects and you need to fill in a few drop-down menus, I would create a view model. I would also create this view model in a web application project, so if your view model has SelectList objects, you don’t need a reference to the MVC dll in your domain model.

-one
source share

All Articles