0

I'm not too sure what the term is as I'm fairly new to ASP.NET MVC EF. I'm trying to display users that I have created with my create actionresult function onto my index view like Name, Work Address, Home Address, Asset Name,etc...

So far I can only get Name and Home Address to display properly by doing this:

 public ActionResult Index()
 {
      var clients = db.Clients.Include(c => c.HomeAddress);
      return View(clients.ToList());
 }

How would I display the other data as well like Asset Name, Work Address and so on?

My Client Model:

public class Client : Person {
    public ICollection<OccupancyHistoryRecord> OccupancyRecords { get; set;     }

        public ICollection<RentHistoryRecord> RentRecords { get; set; }

        public Asset Assets { get; set; }
    }
}

Person Model:

public class Person {
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Display(Name = "Home Address")]
    public FullAddress HomeAddress { get; set; }

    [Display(Name = "Work Address")]
    public FullAddress WorkAddress { get; set; }
}

FullAddress Model:

public Class FullAddress {
    public int Id { get; set; }

    [Display(Name = "Suite")]
    public string UnitNum { get; set; }

    [Display(Name = "Street Address")]
    public string StreetAddress { get; set; }

    public string Province { get; set; }

    public string Country { get; set; }

    [Display(Name = "Postal Code")]
    public string PostalCode { get; set; }
}

Assets Model:

public Class Asset {
    public int Id { get; set; }

    [Display(Name = "Asset Name")]
    public string Name { get; set; }

    [Display(Name = "Asset Type")]
    public string Type { get; set; }

    public FullAddress Address { get; set; }

    [Display(Name = "Asking Rent")]
    public string AskingRent { get; set; }

    public ICollection<OccupancyHistoryRecord> OccupancyRecords;

    public ICollection<RentHistoryRecord> RentRecords;

}

My Index View so far:

@model IEnumerable<RentalManagement.Models.Client>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
    <table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Assets.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Assets.Type)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HomeAddress) 
        </th>
        <th>
            @Html.DisplayNameFor(model => model.WorkAddress)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Assets.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Assets.Type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HomeAddress.StreetAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.WorkAddress)
        </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>
}

Any help is appreciated. Thanks!

1 Answer 1

1

Include all the virtual properties:

EF Core version:

public ActionResult Index()
{
    var clients = db.Clients
        .Include(p => p.Assets)
            .ThenInclude(p => p.Address)
        .Include(p => p.Assets)
            .ThenInclude(p => p.OccupancyRecords)
        .Include(p => p.Assets)
            .ThenInclude(p => p.RentRecords)
        .Include(p => p.HomeAddress)
        .Include(p => p.WorkAddress)
        .ToList();
    return View(clients);
}

EF 6 version:

public ActionResult Index()
{
    var clients = db.Clients
        .Include(p => p.Assets.Select(s => s.Address))
        .Include(p => p.Assets.Select(s => s.OccupancyRecords))
        .Include(p => p.Assets.Select(s => p.RentRecords))
        .Include(p => p.HomeAddress)
        .Include(p => p.WorkAddress)
        .ToList();
    return View(clients);
}

Also mark this properties as virtual to enable lazy loading mechanism:

public virtual ICollection<OccupancyHistoryRecord> OccupancyRecords;
public virtual ICollection<RentHistoryRecord> RentRecords;
Sign up to request clarification or add additional context in comments.

2 Comments

There doesn't seem to be a ThenInclude function for me?
.ThenInclude is a EF Core extension. I edited my answer and posted the EF 6 version also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.