select list usage in asp.net mvc
Asked Answered
S

2

8

I am working on an MVC site that is the first my company has done, one of the things I find myself doing a lot is creating selectlists in a controller, putting them in viewdata and reading them when creating a html.DropDownList. theer are a few things that strike me as smelly about the way i do it.

  1. some pages can repeat lists of things (the board rate of a hotel room wheer a user can add as many rooms to a hotel as they need), currently i use the same selectlist, is this good practice or should they have one each?

  2. with the previous example the "room" is an ascx rendered either by a renderpartial or an ajax call via jquery. what is the best way for the controller to pass the selectlist so the ascx can use it, currently I add to the viewdata for the page, which passes it's viewdata to the renderpartial, but then on the ajax call the action method also needs to add the selectlist to the Viewdata for the ascx, agan not sure this is the best way.

  3. I have a repository that holds this "static" data and returns as a generic list, so each time the controller needs the data it hits the repository for the list (there are a few more than just board rate, things like titles for people, mr, mrs etc and others) I suspect some kind of cache would be better as the data rarely if ever changes.

does anyone have any advice in these areas?

Scriven answered 15/12, 2008 at 13:37 Comment(0)
R
3
  1. If those SelectLists are completely equal, then I'm sure better to use one list for multiple DropDownLists.

  2. Look like its common usage. I'm using the similar approach too, but thinking about to port some of the controls to Html.RenderAction, because passing through page's ViewData to control feels not good for me too. Yep, I know that it will not be pure MVC :)

  3. If your ORM supports cache, of course use it.

But also if your project is not small and you think it will continue to grow, I recommend to implement a Service Layer (PoEAA pattern) above your repositories that will contains business logic and the cache management logic too.

If you want to manage your cache transparently, without affecting your dal or mvc layer, I think that the best approache is to use AOP.

Rosina answered 15/12, 2008 at 14:14 Comment(1)
OK. It's small and immutable, how do I cache it easily?Educatory
R
0

One way of doing this is to put the data you need for the select list in your model via a typed view. The view can iterate over the data in the model to generate any select lists it needs. You can also stuff an object directly into the ViewData collection, but I think having a typed view results in much cleaner code.

Rectal answered 15/12, 2008 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.