1

Little context:

  • I have a link model which has a foreign Key ID to the identifier model and client model.

  • The client model and identifier model have the link model as a virtual Icollection.

  • Both the Client model and the identifier model have a foreign Key ID to another model MJTopics.

I'm using EF6 to create controllers with views and on the details view of the Client controller I can pull information from the links icollection into the view however I want to pull in identifiers which share the same MJTopicsID as the Client as shown below:

Use of the Links Icollection in Client Details:

<h3>Links Built</h3>
    <table class="table">
        <tr>
            <th>Domain</th>
            <th>Page Containing Link</th>
            <th>Anchor Text</th>
            <th>Date</th>
            <th>Action</th>
                </tr>
        @foreach (var item in Model.Links)
        {
        <tr>
                                     <td>
                @Html.DisplayFor(modelItem => item.Identifier.domain)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Obdomain)
            </td>
                            <td>
                @Html.DisplayFor(modelItem => item.Anchor)
            </td>

                      <td>
                @Html.DisplayFor(modelItem => item.BuildDate)
            </td>
                                    <td>
        @Html.ActionLink("Edit", "Edit", "Status", new { id=item.LinkID }) |
        @Html.ActionLink("Domain", "Details", "Status", new { id=item.LinkID }) |
        @Html.ActionLink("Delete", "Delete", "Status", new { id=item.LinkID })
    </td>
        </tr>
        }
    </table>

Possible related domains (I set a viewbag variable for the MJTopics ID of the client):

<h3>Possible Related Domains</h3>
    <table class="table">
        <tr>
            <th>Domain</th>
                            <th>Status</th>
            <th>Date</th>
                            <th>Action</th>
                </tr>
        @foreach (var item in Model.Identifier.Where(p=>p.MJTopics.ID == ViewBag.MjTopicsID))
        {
        <tr>
                                     <td>
                @Html.DisplayFor(modelItem => item.Identifier.domain)
            </td>
                            <td>
                @Html.DisplayFor(modelItem => item.status)
            </td>

                      <td>
                @Html.DisplayFor(modelItem => item.Last)
            </td>
                    <td>
        @Html.ActionLink("Go Live", "Live", "Status", new { id=item.StatusID }) |
        @Html.ActionLink("Edit", "Edit", "Status", new { id=item.StatusID }) |
        @Html.ActionLink("Domain", "Details", "Status", new { id=item.StatusID }) |
        @Html.ActionLink("Delete", "Delete", "Status", new { id=item.StatusID })
    </td>
        </tr>
        }

    </table>

How would I go about doing this? I tried specifying another model in parenthesis but obviously it only allows one model per view, the only other way I have read about is partial views but don't know anything about these as of yet. Is there another simple way?

liamcook
  • 143
  • 2
  • 10
  • 3
    Have you considered creating a View Model that has the properties you need? – devlin carnate Jan 03 '18 at 18:28
  • 2
    Possible duplicate of [Multiple models in a view](https://stackoverflow.com/questions/4764011/multiple-models-in-a-view) – devlin carnate Jan 03 '18 at 18:56
  • That is true but I would like to have it on the same view, could this be done using partial views then? – liamcook Jan 03 '18 at 19:00
  • A [ViewModel](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) can be a composition of other classes thereby satisfying the one-per-page rule. – Jasen Jan 03 '18 at 19:02
  • @jasen I had a go at creating a view model but I get the error of it not containing a key although I read they don't need to? do I initialise the viewmodel as well using the dbset? – liamcook Jan 05 '18 at 11:34
  • The View Model would only contain the properties necessary for the view. The entity properties matching the VM properties don't have to be 1:1. You could also have properties on the VM not on the entity. To populate the VM you'd get an entity from the dbcontext then map the fields you require `vm.Id = e.Id; vm.Name = e.Name;` You'd do the reverse process to populate an entity from the VM. – Jasen Jan 05 '18 at 17:46

1 Answers1

0

This was achieved using a ViewModel!

liamcook
  • 143
  • 2
  • 10