我有這個錯誤了一天以上,我真的不能修復它。我知道網上有很多關於這個話題的問題,我已經反覆閱讀,仍然沒有解決問題。 我只是學習MVC 4,所以我非常困惑。 我收到錯誤消息:MVC 4添加錯誤
具有'cabinCrewId'鍵的ViewData項的類型爲'System.Int32',但必須是'IEnumerable'類型。
任何幫助或方向將不勝感激!
我的控制器:
public ActionResult AddCrew()
{
FlightCabinCrew fcc = new FlightCabinCrew();
return View(fcc);
}
帖子行動:
[HttpPost]
public ActionResult AddCrew(FlightCabinCrew fcc)
{
if (ModelState.IsValid)
{
using (A1Context db = new A1Context())
{
var data = from person in db.person
from flightcrew in db.flightcabincrew
from cabincrew in db.cabincrew
where flightcrew.cabinCrewId == cabincrew.person
where cabincrew.person == person.id
select person.name;
ViewBag.list = new SelectList(data.ToList(), "id", "name");
db.flightcabincrew.Add(fcc);
db.SaveChanges();
return RedirectToAction("Index");
}
}
else
{
using (A1Context db = new A1Context())
{
var data = from person in db.person
from flightcrew in db.flightcabincrew
from cabincrew in db.cabincrew
where flightcrew.cabinCrewId == cabincrew.person
where cabincrew.person == person.id
select person.name;
ViewBag.list = new SelectList(data.ToList(), "name", "name");
return View(fcc);
}
}
}
}
而我的觀點:
<div class="editor-label">
@Html.LabelFor(model => model.cabinCrewId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.cabinCrewId, (SelectList)ViewBag.list)
@Html.ValidationMessageFor(model => model.cabinCrewId)
</div>
感謝
我知道我需要分配的SelectList到ViewBag在GET AddCre中w方法(正如我在POST方法中所做的那樣)。但我確實知道GET方法和我放入的東西是多麼的令人興奮。
如這裏要求的是人類
[Table("person")]
public class Person
{
[Key, Column(Order = 1)]
public int id { get; set; }
public string name { get; set; }
}
您可以添加「person」類的代碼嗎? – ekad 2014-09-26 01:48:57
@ekad這裏是 – user398239 2014-09-26 01:51:45
[MVC 4下拉框錯誤]的可能的副本(http://stackoverflow.com/questions/26005982/mvc-4-drop-down-box-error) – Leo 2014-09-26 01:57:47