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!