这是我第一次编写MVC 5应用程序并使用C#。我已经走得很远了,并且能够解决我遇到过的一些错误,但是我很难接受这个错误。
我设置了一些模型和表,包括外键引用。在我的索引页面上,每条记录都显示带有以下控制器代码的外键值:
索引控制器
public ActionResult Index(int? filterDwg)
{
var checkOut = db.CheckOut.Include(c => c.CheckOutStatus).Include(c => c.DwgList);
var checkedOut = checkOut.Where(s => s.CheckStatus == (1));
//Filtering by Drawing
if (filterDwg != null)
{
checkedOut = checkedOut.Where(d => d.DrawingId == (filterDwg));
}
return View(checkedOut.ToList());
}
现在,我的详细信息页面,我的控制器如下:
详情控制器
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CheckOut checkOut = db.CheckOut.Find(id);
if (checkOut == null)
{
return HttpNotFound();
}
return View(checkOut);
}
当我拉出以下视图时:
详细信息视图
@model NoDwgASP.Models.CheckOut
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>CheckOut</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.CheckOutStatus.CheckStatusName)
</dt>
<dd>
@Html.DisplayFor(model => model.CheckOutStatus.CheckStatusName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.DwgList.DwgNum)
</dt>
<dd>
@Html.DisplayFor(model => model.DwgList.DwgNum)
</dd>
<dt>
@Html.DisplayNameFor(model => model.UserInitials)
</dt>
<dd>
@Html.DisplayFor(model => model.UserInitials)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Project)
</dt>
<dd>
@Html.DisplayFor(model => model.Project)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CheckOutDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CheckOutDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.ReturnDate)
</dt>
<dd>
@Html.DisplayFor(model => model.ReturnDate)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.CheckId }) |
@Html.ActionLink("Back to List", "Index")
</p>
DwgNum和CheckStatusName的字段为空。这些字段是整数,是与另一个具有这些属性的字符串值的数据库绑定的外键。
我意识到我需要像
这样的东西var checkOut = db.CheckOut.Include(c => c.CheckOutStatus).Include(c => c.DwgList)
然而,正如我在索引控制器中所做的那样,我不能简单地将.Include附加到
CheckOut checkOut = db.CheckOut.Find(id);
有没有人有任何建议?我很难过,而且我找不到任何相关的问题。
答案 0 :(得分:0)
如上所述..
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CheckOut checkOut = db.CheckOut
.Include(c => c.CheckOutStatus)
.Include(c => c.DwgList)
.SingleOrDefault(x => x.CheckId == id);
if (checkOut == null)
{
return HttpNotFound();
}
return View(checkOut);
}