Search code examples
linq-to-sqlasp.net-mvc-2

Can't load a DropDownList with values


I'm following the NerdDinner tutorial while building my own application.

The tutorial gives this:

public ActionResult Edit(int id) {
 
Dinner dinner = dinnerRepository.GetDinner(id);
 
ViewData["Countries"] = new SelectList(PhoneValidator.AllCountries, dinner.Country);
 
return View(dinner);
}

My code is like this:

public ActionResult Edit(int id)
        {
            Area area = areaRepository.GetArea(id);
            JefeRepository jefe = new JefeRepository();
            ViewData["Jefes"] = new SelectList(jefe.FindAllJefes(), area.Jefe.Nombre);
            return View(area);
        }

The FindAllJefes() returns a IQueryable collection. Right now when I run the application the dropdownlist loads with two items: SeguimientoDocente.Jefe and SeguimientoDocente.Jefe.

It seems it's loading the type and not the actual value I want.

Edit

I just realized this isn't a bug. The code is doing exactly what I'm telling it to do. What would be the most efficient way of retrieving the name of each Jefe?


Solution

  • Use another constructor for SelectList where you specify which property of SeguimientoDocente should be a value and which property should be displayed as text. Something like:

    ViewData["Jefes"] = new SelectList(jefe.FindAllJefes(), "Id", "Name", area.Jefe.Nombre);