Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select list usage in asp.net mvc

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?

like image 350
Pharabus Avatar asked Dec 15 '08 13:12

Pharabus


People also ask

What is select list in MVC?

SelectList(IEnumerable, String, String, Object, IEnumerable) Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, the data text field, the selected value, and the disabled values.


1 Answers

  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.

like image 95
maxnk Avatar answered Nov 15 '22 05:11

maxnk