Domanda

This is the setup:

Model:

public class Device
{
    [Key]
    public int Id { get; set; }
    [MaxLength(50)]
    public String Name { get; set; }
    public Category Category { get; set; }
    public Manufactor Manufactor { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Status> Status { get; set; }
}

ControllerAction:

public ActionResult Index()
{
   var devices = db.Devices.Include("Categories").Include("Manufactors").Select(x => new Device
                   {
                        Name = x.Name,
                        Category = x.Category,
                        Comments = x.Comments,
                        Manufactor = x.Manufactor,
                        Status = x.Status
                    });
    return View(db.Devices.ToList());
}

View:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Manufactor.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Status)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

The problem here is, that only name gets displayed, but item.Category and item.Manufactor not. If I change the line to simply @item.Category.Name I am running into a Nullreference Exception.

Looking into my database into the device table there are 2 devices listed, with each of those having a CategoryId and ManufactorId in it.

I deemed this a 20 minute no-brainer, but it seems I went terribly wrong somewhere. Please help me solve this error.

If you need additional code, just post it in the comments.

Kind regards

EDIT: In response to those very helpful answers: the error was my faulty controller action. This solution worked:

return View(db.Devices.Include(d => d.Manufactor).Include(d => d.Category).ToList());
È stato utile?

Soluzione

Your model property is called Category, not Categories, so make sure you are making the proper Include:

db.Devices.Include("Category")...

Same stands true for the Manufacturers of course. Your model property's called Manufactor and not Manufactors so include the correct one:

.Include("Manufactor")

Now EF will make proper joins in the tables and hydrate your Category and Manufactor properties that you could use in your view.

Altri suggerimenti

In addition to Darin's answer: there is also a strong-typed variant of the Include() method available which will warn you at compile-time when you use non-existing properties:

db.Devices.Include(d => d.Category)

Reference: DbExtensions.Include()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top