I have a simple Model:
public partial class Entidade
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Nome { get; set; }
public virtual Pais Pais { get; set; }
}
related with the Pais model:
public partial class Pais
{
public Pais()
{
this.Entidades = new HashSet<Entidade>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Nome { get; set; }
public virtual ICollection<Entidade> Entidades { get; set; }
}
and I need to build a simple dropdown list of the Pais's to create a new Entidade:
<div class="editor-label">
@Html.LabelFor(model => model.Pais, "Pais")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Pais, ViewBag.Paises as SelectList)
@Html.ValidationMessageFor(model => model.Pais)
</div>
from the controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Entidade entidade)
{
if (ModelState.IsValid)
{
db.Entidades.Add(entidade);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Paises = new SelectList(db.Paises, "Id", "Nome");
return View(entidade);
}
But I get the error:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Pais'.
Any idea?
Thanks
You are creating adrop down for a Pais entity not for it's Id, so:
@Html.DropDownListFor(model => model.Pais.Id, ViewBag.Paises as SelectList)
But this will end up in a NullReferenceException if you aren't setting the model Pais property to an instance.
Addtitional tip: if you can try avoid using ViewBag or ViewData. In this sitiation you can create a EntidadeViewModel class even derived from Entidade and this can have a property of type IEnumerable. In this way your code will be more readable and consistent.
Try
ViewBag.Paises = db.Paises.ToList();
@Html.DropDownList("Id",new SelectList(ViewBag.Paises,"Id","Nome"))
Do it like this:
Pais.cs
public Pais()
{
Entidades = new HashSet<Entidade>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Nome { get; set; }
public virtual ICollection<Entidade> Entidades { get; set; }
Entidade.cs
public class Entidade
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Nome { get; set; }
[ForeignKey("Pais")]
public int PaisId { get; set; }
public virtual Pais Pais { get; set; }
}
EntidateController.cs
//GET: /Entidade/Create
public ActionResult Create()
{
ViewBag.PaisId = new SelectList(db.Paises, "Id", "Nome");
return View();
}
// POST: /Entidade/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Entidade entidade)
{
if (ModelState.IsValid)
{
db.Entidades.Add(entidade);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PaisId = new SelectList(db.Paises, "Id", "Nome", entidade.PaisId);
return View(entidade);
}
Entidate Create.cshtml
@Html.DropDownList("PaisId", String.Empty)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.