Search code examples
c#asp.net-mvcdrop-down-menuviewdata

passing values to a dropdownlist in an mvc view


I am a newb to MVC programming. I am passing values from other db tables to my edit and create views to populate some dropdownlists. It's working great. I have code like this in my controller for edit and create:

var db = new MyProgramDataContext();
Order order = orderRepository.GetOrder(id);
ViewData["customer"] = from c in db.customers
                       select new SelectListItem
                       {
                              Text = c.customer_name,
                              Value = c.customer_name
                       }
return View(order);

I want to move the select statement to the Respository to make things a bit cleaner so that I'm not repeating the same selects in Edit and Create.

ViewData["customer"] = orderRepository.GetCustomers();

In the Repository, should the return type of GetCustomers be SelectListItem? I can't seem to get it to work.


Solution

  • I now have my SelectLists in a FormViewModel class and am using partial views for my edit and create code. Thanks everyone.