我试图从一个数据库表中填充一个下拉列表,
我得到了一个错误:
没有一个类型为'IEnumerable‘的IEnumerable条目具有'QuartierId’键。 描述:一个例外,非gérée‘s’‘est produite au moment de l’eécution de la requ actuelle。电话: trace la trace la pile pour dans d‘’informations l‘’erreur et son origine dans le code。 System.InvalidOperationException:没有一个类型为'IEnumerable‘的ViewData条目具有'QuartierId’键。
这里我的模型"QuartierSet":
public partial class Quartier
{
public Quartier()
{
this.Publication = new HashSet<Publication>();
}
public int Quartier_Id { get; set; }
public string Quartier_Libelle { get; set; }
public virtual ICollection<Publication> Publication { get; set; }
public virtual Ville Ville { get; set; }
}我的主计长方面:
public ActionResult CreateOffreLocation(OffreLocation offreLocation)
{
try
{
ViewBag.QuartierId = new SelectList(db.QuartierSet, "Quartier_Id", "Quartier_Libelle");
db.PublicationSet.Add(offreLocation);
db.SaveChanges();
return RedirectToAction("ListOffreLocation");
}
catch
{
return RedirectToAction("Error");
}
}我的观点是:
@Html.DropDownList("QuartierId", String.Empty)发布于 2014-05-15 15:05:09
ViewBag只能从您将其设置为其操作视图的操作中获得。
在ViewBag中设置之后,您将重定向到另一个操作,由于该操作,ViewBag再次设置为null。
您需要在ListOffreLocation操作中添加以下行,而不是CreateOffreLocation
ViewBag.QuartierId = new SelectList(db.QuartierSet, "Quartier_Id", "Quartier_Libelle");发布于 2014-05-15 15:32:35
@Html.DropDownList((SelectListItem)ViewBag.QuartierId, String.Empty)https://stackoverflow.com/questions/23682048
复制相似问题