I have defined a model, Item, which has the following properties:
public class Item
{
public bool Active { get; set; }
public string ItemCode { get; set; }
public int ItemId { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }
}
I am trying to reference the Category from within the detail view on the Item controller. My code in the controller is:
public ActionResult Details(int id = 0)
{
Item item = db.Items.Find(id);
if (item == null)
{
return HttpNotFound();
}
return View(item);
}
However, when I explore the item object here I can see the CategoryId is correctly set, the Category property is set to null. Therefore when I reference Item.Category.Name I am getting a blank result. I have tried making my properties virtual (following a tutorial on-line) but this didn't seem to fix the issue. What am I doing wrong?