1
I’m having the following error when turning:
System.Invalidoperationexception: 'There is no 'Ienumerable' Viewdata item that has the 'Sexoid' key'.'
Follows code below:
Model:
[Required(ErrorMessage = "O campo sexo é obrigatório!")]
public int SexoID { get; set; }
Controller:
public ActionResult DropDown()
{
var model = new CadastroModel();
ViewBag.Sexo = new List<SelectListItem>
{
new SelectListItem { Text = "Selecione", Value="", Selected = true},
new SelectListItem { Text = "Masculino", Value="1"},
new SelectListItem { Text = "Feminino", Value="2"},
};
return View(CadastrarUsuario);
}
[HttpPost]
public ActionResult DropDown(CadastroModel CadastrarUsuario)
{
if (ModelState.IsValid)
{
return View(CadastrarUsuario);
}
ViewBag.Sexo = new List<SelectListItem>
{
new SelectListItem { Text = "Selecione", Value="", Selected = true},
new SelectListItem { Text = "Masculino", Value="1"},
new SelectListItem { Text = "Feminino", Value="2"},
};
return View(CadastrarUsuario);
}
View:
<div class="form-group col-md-3">
<div class="editor-label col-md-3">
@Html.LabelFor(m => m.SexoID)
<div class="col-md-3">
@Html.DropDownListFor(e => e.SexoID, ViewBag.Sexo as IEnumerable<SelectListItem>, "Selecione")
@Html.ValidationMessageFor(e => e.SexoID)
</div>
</div>
</div>