" that has the key "Carrera", I am having problems processing a send request ...">

There is no ViewData element of type "IEnumerable <SelectListItem>" that has the key "Carrera",

I am having problems processing a send request for my controller:

[HttpGet]
public ActionResult Crear()
{
    CarreraRepository carreraRepository = new CarreraRepository();
    var carreras = carreraRepository.FindAll().OrderBy(x => x.Nombre);
    var carrerasList = new SelectList(carreras, "ID", "Nombre");
    ViewData["Carreras"] = carrerasList;

    Materia materia = new Materia();
    return View(materia);        
}

[HttpPost]
public ActionResult Crear(Materia materia, FormCollection values)
{
    if (ModelState.IsValid)
    {
        repo.Add(materia);
        repo.Save();

        return RedirectToAction("Index");
    }
    return View(materia);
}

When the HttpGet action starts, the form for creating the rendering is excellent. The values ​​are correctly set to DropDownList, and all peach; when I try to submit the form (run the HttpPost action), I get an error.

Can anyone help me out?

Is it because HttpPost doesn't have a ViewData declaration? Thanks for the help.

+5
source share
1 answer

, Creat, ViewData["Carreras"] . carreras .

[HttpPost]
public ActionResult Crear(Materia materia, FormCollection values)
{
    CarreraRepository carreraRepository = new CarreraRepository();
    var carreras = carreraRepository.FindAll().OrderBy(x => x.Nombre);
    var carrerasList = new SelectList(carreras, "ID", "Nombre");
    ViewData["Carreras"] = carrerasList;

    if (ModelState.IsValid)
    {
        repo.Add(materia);
        repo.Save();

        return RedirectToAction("Index");
    }
    return View(materia);
}
+16

All Articles