.NET在Razor View中使用另一个不相关的模型

时间:2018-01-03 18:24:15

标签: c# asp.net .net entity-framework

小背景:

  • 我有一个链接模型,它具有标识符模型和客户端模型的外键ID。

  • 客户端模型和标识符模型将链接模型作为虚拟Icollection。

  • 客户端模型和标识符模型都具有另一个模型MJTopics的外键ID。

我使用EF6创建带视图的控制器,在客户端控制器的详细信息视图中,我可以将链接icollection中的信息提取到视图中,但是我想提取与客户端共享相同MJTopicsID的标识符如下图所示:

在客户详细信息中使用链接Icollection:

<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>

可能的相关域(我为客户端的MJTopics ID设置了一个viewbag变量):

<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>

我该怎么做呢?我尝试在括号中指定另一个模型,但显然每个视图只允许一个模型,我读过的唯一另一种方法是部分视图,但对此尚未了解。还有另一种简单的方法吗?

1 个答案:

答案 0 :(得分:0)

这是使用ViewModel实现的!