I have a database with sites, which I retrieve to save them in a database of billboards. I use DropDownList to recover the values, but when I try to save, it says:
Server error in the application '/'. There is no element ViewData of type 'IEnumerable' with the key 'IdSede'. Description: Unhandled exception when executing the Web request current. Check the stack trace for more information about the error and where it originated in the code.
Exception details: System.InvalidOperationException: There is no No ViewData element of type 'IEnumerable' with the 'IdSede' key.
Source code error:
Line 18: Line 19:
Headquarters Line 20:
@ Html.DropDownList ("IdSede", null, "Select Headquarters", new {@class = "form-control"}) Line 21:
@ Html.ValidationMessageFor (model = > model.IdSede, "", new {@class = "text-danger"}) Line 22:
Driver :
public ActionResult NuevoCartelera()
{
ViewBag.IdSede = new SelectList(db.Sedes, "IdSede", "Nombre");
ViewBag.IdPelicula = new SelectList(db.Peliculas, "IdPelicula", "Nombre");
ViewBag.IdVersion = new SelectList(db.Versiones, "IdVersion", "Nombre");
return View();
}
[HttpPost]
public ActionResult NuevoCartelera(Carteleras Cartelera)
{
Carteleras car = new Carteleras();
car.IdSede = Cartelera.IdSede;
car.IdPelicula = Cartelera.IdPelicula;
car.HoraInicio = Cartelera.HoraInicio;
car.FechaInicio = Cartelera.FechaInicio;
car.FechaFin = Cartelera.FechaFin;
car.NumeroSala = Cartelera.NumeroSala;
car.IdVersion = Cartelera.IdVersion;
car.Lunes = Cartelera.Lunes;
car.Martes = Cartelera.Martes;
car.Miercoles = Cartelera.Miercoles;
car.Jueves = Cartelera.Jueves;
car.Viernes = Cartelera.Viernes;
car.Sabado = Cartelera.Sabado;
car.Domingo = Cartelera.Domingo;
if (ModelState.IsValid)
{
db.Carteleras.Add(car);
db.SaveChanges();
return RedirectToAction("Carteleras"); // Retorna a la vista "Peliculas"
}
return View();
}
Vista :
model Cinemania.Carteleras
@{
Layout = "/Views/Shared/_LayoutAdministracion.cshtml";
}
<div class="container">
@*using (Html.BeginForm("/NuevoPelicula", "Administracion", new { enctype = "multipart/form-data", @class = "text-danger" }))*@
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<!-- CABECERA -->
<h2 class="text-center">Nueva cartelera</h2>
<!-- INPUT NOMBRE -->
<div class="form-group">
<label for="Sede">Sede</label>
@*Html.DropDownList("IdSede", null, "Selecione Sede", new { @class = "form-control" })*@
@Html.DropDownList("IdSede", (IEnumerable<SelectListItem>)ViewBag.IdSede, "Selecione Sede", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IdSede, "", new { @class = "text-danger" })
</div>
<!-- INPUT DESCRIPCION -->
<div class="form-group">
<label for="Sede">Pelicula</label>
@Html.DropDownList("IdPelicula", null, "Selecione Pelicula", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IdPelicula, "", new { @class = "text-danger" })
</div>
<!-- INPUT CALIFICACION -->
<div class="form-group">
@Html.LabelFor(model => model.HoraInicio, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.HoraInicio, new { @class = "form-control horario time ui-timepicker-input" })
@Html.ValidationMessageFor(model => model.HoraInicio, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label for="lunes">Lunes</label>
@Html.CheckBox("lunes", true)
<label for="martes">Martes</label>
@Html.CheckBox("martes", true)
<label for="miercoles">Miercoles</label>
@Html.CheckBox("miercoles", true)
<label for="jueves">Jueves</label>
@Html.CheckBox("jueves", true)
<label for="viernes">Viernes</label>
@Html.CheckBox("viernes", true)
<label for="sabado">Sabado</label>
@Html.CheckBox("sabado", true)
<label for="domingo">Domingo</label>
@Html.CheckBox("domingo", true)
</div>
<div class="form-group">
@Html.LabelFor(model => model.FechaFin, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FechaFin, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FechaFin, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FechaInicio, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FechaInicio, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FechaInicio, "", new { @class = "text-danger" })
</div>
</div>
<!-- INPUT DURACION -->
<div class="form-group">
<label for="sala">Numero de sala</label>
@Html.EditorFor(model => model.NumeroSala, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NumeroSala, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label for="version">Version</label>
@Html.DropDownList("IdVersion", null, "Selecione version", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IdVersion, "", new { @class = "text-danger" })
</div>
<!-- BOTON GUARDAR -->
<div class="text-center">
<input type="submit" class="btn btn-success" value="Guardar" />
<!-- LINK SALIR -->
<a href="./Peliculas" class="btn btn-default">Cancelar</a>
</div>
</div>
}
</div>
<script src="~/Scripts/jquery-3.1.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<link href="~/Content/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui.min.js"></script>
<link href="~/Content/jquery.timepicker.css" rel="stylesheet" />
<script src="~/Scripts/jquery.timepicker.js"></script>
<script src="~/Scripts/Fecha.js"></script>
<script src="~/Scripts/Horario.js"></script>
</body>
</html
Model :
namespace Cinemania.Models
{
public class CarteleraMetaData
{
public int IdCartelera { get; set; }
[Required(ErrorMessage = "Debe elegir una sede")]
public int IdSede { get; set; }
[Required(ErrorMessage = "Debe elegir una pelicula")]
public int IdPelicula { get; set; }
[Required(ErrorMessage = "Debe indicar la hora de inicio")]
//[RegularExpression(@"^(0[1-9]|1[0-2]):[0-5][0-9] [AP]M$", ErrorMessage = "Se debe seleccionar un horario válido")]
public string HoraInicio { get; set; }
/* public TimeSpan _HoraInicio
{
get
{
try
{
DateTime dt = DateTime.Parse(HoraInicio);
return dt.TimeOfDay;
}
catch
{
return new TimeSpan();
}
}
}*/
[Required(ErrorMessage = "Debe indicar la fecha de inicio")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]
public Nullable<System.DateTime> FechaInicio { get; set; }
[Required(ErrorMessage = "Debe indicar la fecha de fin")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]
public Nullable<System.DateTime> FechaFin { get; set; }
[Required(ErrorMessage = "Debe indicar el numero de sala")]
public int NumeroSala { get; set; }
public bool Lunes { get; set; }
public bool Martes { get; set; }
public bool Miercoles { get; set; }
public bool Jueves { get; set; }
public bool Viernes { get; set; }
public bool Sabado { get; set; }
public bool Domingo { get; set; }
[Required(ErrorMessage = "Debe indicar la version de la pelicula")]
public int IdVersion { get; set; }
}
}
I have the other DropDownList
that go perfectly.