我有这个错误了一天以上,我真的不能修复它。我知道网上有很多关于这个话题的问题,我已经反复阅读,仍然没有解决问题。 我只是学习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