This is my first time both writing an MVC 5 application and using C#. I've gotten pretty far along, and have been able to trouble shoot a number of bugs I've come across, but I'm stumped on this one.
I have a few models and tables set up, including foreign key references. On my index pages, each record displays the foreign key values with the following controller code:
Index controller
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());
}
Now, my details page, my controller is as follows:
Details Controller
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);
}
When i pull up the following view:
Details View
@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>
The fields for DwgNum and CheckStatusName are blank. These fields are integers and are foreign keys tied to another database that has string values for those properties.
I realize I need to so something like
var checkOut = db.CheckOut.Include(c => c.CheckOutStatus).Include(c => c.DwgList)
as I did in my index controller, however, I cant simply append an .Include onto
CheckOut checkOut = db.CheckOut.Find(id);
Does anyone have any suggestions? I'm stumped, and I havent been able to find any related problems.