locked
Not Sure On Approach to Take RRS feed

  • Question

  • User-692821018 posted

    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?

    Monday, April 24, 2017 10:04 PM

Answers

  • User1068175894 posted

    There are a couple of issues here:

    You have to cast ViewBag.ContactDetails to SelectList because it is a dynamic property as:

    @foreach (var item in (SelectList)ViewBag.ContactDetail)

    But really you normally use the SelectList for dropdowns to do something like:

    @Html.DropDownListFor(m => m.ThingId, (SelectList)ViewBag.ListOfThings, "Select one")

    Where ThingId is the property you want to modify in your viewmodel and List of things is the possible Values (id,name) for it

    By the way once you wrap your list in a SelectList the properties are not called iContactType and ContactDetail anymore but "Text" and "Value"

    If you are not using a Dropdown you dont need to wrap your list in a SelectList

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 27, 2017 9:25 PM

All replies

  • User1068175894 posted

    There are a couple of issues here:

    You have to cast ViewBag.ContactDetails to SelectList because it is a dynamic property as:

    @foreach (var item in (SelectList)ViewBag.ContactDetail)

    But really you normally use the SelectList for dropdowns to do something like:

    @Html.DropDownListFor(m => m.ThingId, (SelectList)ViewBag.ListOfThings, "Select one")

    Where ThingId is the property you want to modify in your viewmodel and List of things is the possible Values (id,name) for it

    By the way once you wrap your list in a SelectList the properties are not called iContactType and ContactDetail anymore but "Text" and "Value"

    If you are not using a Dropdown you dont need to wrap your list in a SelectList

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 27, 2017 9:25 PM
  • User-692821018 posted

    Thanks for the help. 

    I am still feeling my way in this a bit, so I guess errors are going to be common.

    I made a work around that works for this.  I made two ViewComponents, one that lists the contact person, the second lists all their contact mechanisms.  This works grossly how I intended.

    I forgot to mark this as solved.  Sorry

    Thursday, April 27, 2017 9:48 PM
  • User1068175894 posted

    You are welcome. Think about creating a ViewModel that represents exactly what you want to show on the screen; it will simplify your rendering instead of using the Domain objects directly in the Views

    Friday, April 28, 2017 3:02 PM