tags:

views:

46

answers:

1

I have read various articles about the modal state that is used in the ASP.NET MVC. I have read this article link text from Scott Gu. I have some code in my controllers that are hitting the database every time a select list is need to display an error. Is there any way to have the modal state save the lists contents? Or maybe cache the lists?

// controller code 
// re display the error do to a business rule violation
_ratesViewData.FSCCOde = getFscCode(_rateService.GetFscCode());

 // controller code 
 private SelectList getFscCode(IEnumerable items, object selectedValue)
 {
    return new SelectList(items, "FscID", "FscCode", selectedValue);
 }

// ASP.NET MVC control code   
// ASP.NET MVC control code  
// ASP.NET MVC control code  
Fuel Surcharge Code
<%=Html.DropDownList( "FscCode", "No Fuel Surcharge")%
+1  A: 

First, re-displaying a view because of an error is typically an uncommon operation. I would want to spend my energy optimizing more common operations, like displaying the view in the first place. Second, given that the typical drop-down combo box has, at most, a few dozen items, I would wonder if there's not a deeper problem if this is actually an optimization issue because of database load. Third, caching the lists is fine if you determine that this is actually a performance issue, but I wouldn't optimize anything until you are positive it is a problem.

Craig Stuntz
I am currently refactoring our messy controller. I am adding the modal state to our actions. I am trying to see if the modal state will hold our list of items? Or do I have to hit the database everytime I need a list? With Html.Textboxes() they persist the invalid values with the modal state. I don't see the list being persisted I only see the selected ID from the modal binder
`ModelState` is not a cache. As you note, it's for handling user input, valid or invalid. Since a POST does not submit a `SelectList` (and cannot, really), it will never be in `ModelState`. If you initially populate the select list from the database, I would recommend that you also do so when re-displaying in the case of an error. This allows you to share code. If the initial GET is fast enough then this will be fast enough, too.
Craig Stuntz