脚手架 model/view:具有键 'COLUMN' 的 ViewData 项是 'System.String' 类型,但必须是 'IEnumerable<SelectListItem>' 类型
Scaffolded model/view: The ViewData item that has the key 'COLUMN' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'
我知道已经有 a questions asked similar 了,但他们似乎都在处理加载正常但在尝试 POST 时出现问题的页面。我在带有代码优先数据库的 MVC 项目中使用脚手架代码。失败的下拉列表应该从外键关系中填充,还有另外两个下拉列表使用完全相同的代码但名称不同,评论显示只有这个下拉列表是错误的。这应该在编辑中,但页面甚至不会像其他问题那样加载以提交数据。创建页面和编辑页面均未加载。
综上所述,这是控制器:
public ActionResult Edit(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
ViewBag.Title = new SelectList(db.Job_Title, "Id", "Title", person.Title);
ViewBag.Location = new SelectList(db.Locations, "Id", "Name", person.Location);
ViewBag.Manager = new SelectList(db.Managers, "Id", "Name", person.Manager);
return View(person);
}
这是相应视图的一部分:
<div class="form-group">
@Html.LabelFor(model => model.Title, "Title", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*FIXME: Commenting the line below will allow the page to load fine*@
@Html.DropDownList("Title", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, "Location", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Location", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Manager, "Manager", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Manager", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Manager, "", new { @class = "text-danger" })
</div>
</div>
我很想知道为什么只有标题下拉菜单会杀死页面而其他 2 个完全没问题。我也很想知道为什么生成的脚手架代码出错!
错误意味着 ViewBag.Title
的值是 null
或者不是 IEnumerable<SelectListItem>
。
这将会发生,因为您的视图包括
@{
ViewBag.Title = ".....";
@
这会覆盖您在 GET 方法中设置的值。
我知道已经有 a
综上所述,这是控制器:
public ActionResult Edit(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
ViewBag.Title = new SelectList(db.Job_Title, "Id", "Title", person.Title);
ViewBag.Location = new SelectList(db.Locations, "Id", "Name", person.Location);
ViewBag.Manager = new SelectList(db.Managers, "Id", "Name", person.Manager);
return View(person);
}
这是相应视图的一部分:
<div class="form-group">
@Html.LabelFor(model => model.Title, "Title", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*FIXME: Commenting the line below will allow the page to load fine*@
@Html.DropDownList("Title", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, "Location", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Location", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Manager, "Manager", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Manager", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Manager, "", new { @class = "text-danger" })
</div>
</div>
我很想知道为什么只有标题下拉菜单会杀死页面而其他 2 个完全没问题。我也很想知道为什么生成的脚手架代码出错!
错误意味着 ViewBag.Title
的值是 null
或者不是 IEnumerable<SelectListItem>
。
这将会发生,因为您的视图包括
@{
ViewBag.Title = ".....";
@
这会覆盖您在 GET 方法中设置的值。