[RESOLVED]Refactoring code please advise

For my dropdownlists I store them in ViewData.

eg. ViewData["CustomerList"]

It gets used in several places within the controller. 

eg. Create and Edit

Should I fill the ViewData within each method like this?

Action Create(){

ViewData["CustomerList"] = repository.GetCustomers();


Action Edit(){

ViewData["CustomerList"] = repository.GetCustomers();


Or is it a smarter approach, to only call once? 

I don’t want to repeat code in several places.

Or should I be placing the dropdownlist in ViewModel?



hould I fill the ViewData within each method like this?

Or create a viewmodel. http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/

Hi alphabeatsco,

For an ASP.NET MVC project, there isn’t the way to keep page data. So we need specify the necessary data in the actions.

You could have a common method to set the data.

Best Regards


Hi alphabeatsco,

If it is for Create and Edit Methods you can combine them and create a single Method like

public ActionResult CreateEdit()
   //ViewData["CustomerList"] = ...

but since Create and Edit may contain ViewData for same dropdown but in different way, repeating is necessary.

Hope This Helps

Have static function to load the CustomerList into viewdata and call that function everywhere.
if you have base controller add that static to that class. or have seperate static class

Leave a Reply