I have a model
public class ClientContact
{
public int ClientContactID { get; set; }
[Display(Name ="Client")]
public int ClientID { get; set; }
[Display(Name = "Contact Name")]
public string ContactName { get; set; }
[Display(Name = "Position")]
public int PositionID { get; set; }
[Display(Name = "Contact Type")]
public int ContactTypeID { get; set; }
[Display(Name = "Contact Detail")]
public string ContactDetail { get; set; }
public Client Client { get; set; }
public Position Position { get; set; }
public ContactType ContactType { get; set; }
}This captures all the various contacts a client rep may have. In todays world this is mobile, landline, email and so on. Who knows what tomorrow.
In the details view view I want to create a table showing all the various contact data for this contact. A bit like:
Tony
Email: tony@something.com
phone: 08 9123 4567
mobile: 0412 345 678
I am a little unsure how to approach this. I tried something:
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var clientContact = await _context.ClientContacts
.Include(c => c.Client)
.Include(c => c.ContactType)
.Include(c => c.Position)
.AsNoTracking()
.SingleOrDefaultAsync(m => m.ClientContactID == id);
ViewBag["ContactDetail"] = new SelectList(_context.ClientContacts.Include(c => c.ContactType).Where(c => c.ClientContactID == id), "ContactType", "iContactType");
if (clientContact == null)
{
return NotFound();
}
return View(clientContact);
}My idea was Viewbag would contain the data I am wanting and the view could take it in. But this isn't working, or is simply wrongly formatted.
View:
<dt>
@Html.DisplayNameFor(model => model.Position.iPosition)</dt><dd>
@Html.DisplayFor(model => model.Position.iPosition)</dd><dd><table class="table"><tr><th>
Contact Type</th><th>
Contact Details</th></tr>
@foreach (var item in ViewBag.ContactDetail)
{
<tr><td>
@Html.DisplayFor(modelItem => item.iContactType)</td><td>
@Html.DisplayFor(modelItem => item.ContactDetail)</td><td>I assume by the pretty red lines this isn't correct approach.
How do you loop through a data set like this? Clearly this should be semi simple, what am I missing?