views:

341

answers:

2

I have an ASP.NET MVC application with quite a few drop-down lists and multi-select lists. Essentially, a lot of 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 as I don't really need them on the model and they seem potentially bulky for passing around on the model (I get the selected item or items, which is really all I need). On the downside, ViewData needs casting on the View, which isn't as nice as the strongly typed model.

Is there a best practice here? Even suggestions of pros and cons for either of these would be appreciated.

+4  A: 

I recommend you to use ViewModels to pass that data. It's error prone to use ViewData with "magic strings" and I prefer to use intellisense instead of trying to remember that "magic strings". And you don't need to create that SelectLists in the controller. Just use some IEnumerable and use ToSelectList extension method from MvcContrib in the view.

zihotki
+1 As a matter of convention, we've done everything we can to remove magic strings. Not only for what you mentioned, because intellisense is fantastic, but the real kicker is that magic strings always compile fine. Then they have the opportunity to blow up at runtime. Ouch... Strongly typed things blow up at compile time, which makes errors much easier to detect.
Craig Huber
A: 

I tend to use ViewData if I only have 1 item I'm passing to the view. So, if you're sending multiple objects and need to populate multiple dropdowns I would create a view model. I would also create that view model in the web app project so if your view model has SelectList objects you won't need a reference to the MVC dll in your domain model.

DM