2

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.

mecasull
  • 31
  • 3
  • 1
    You can use `db.CheckOut.Include(....).Where(x => x.ID == id).FirstOrDefault();` (assuming `ID` is your PK field) –  Feb 01 '17 at 02:25
  • Thank you! That worked... I was on my way back to answer my own because i had the idea to search for "combine include and find MVC 5" after i posted.... http://stackoverflow.com/questions/7348663/c-sharp-entity-framework-how-can-i-combine-a-find-and-include-on-a-model-obje – mecasull Feb 01 '17 at 02:28
  • Find method does not support Include. It may be because Find first tries to look for entities loaded in memory and then tries to goes back to database, so you have to use Where as suggested by Stephen. – Vinod Feb 01 '17 at 02:28

1 Answers1

0

as mentioned above..

    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);
}
mecasull
  • 31
  • 3