可以直接访问而不是使用HTML时查看PartialView。

时间:2018-08-20 21:33:24

标签: asp.net-mvc entity-framework

我有这样的局部视图,应该可以为我提供请求的所有相关记录:

public ActionResult DetailsList(short? id)
        {
            var requestDetails = db.RequestDetails.Where(i => i.RequestID == id);
            return View(requestDetails.ToList());

        }

访问时会加载特定ID的所有记录(在这种情况下,请求编号为24)

enter image description here

我试图将其作为部分视图嵌入到我的“请求”主页面,如下所示:

@model CollectionMGR.Models.Request

@{
    ViewBag.Title = "Collection Creation Request Details";
}

<h2>Details</h2>

<div>
    <h4>Request Overview</h4>
    <hr /><p>You can view detailed info about your request here</p>
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.User)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.User)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.AppName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.AppName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.PackageName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.PackageName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Action)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Action)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.CollectionID)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.CollectionID)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Computers)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Computers)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Disposed)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Disposed)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.RequestID }) |
    @Html.ActionLink("Back to List", "Index")

    <h3>@ViewBag.DetailStatus</h3>

    @if (ViewBag.DetailStatus == "No detailed records found")
    {
    <p>no stuff to show</p>
}
else
{
    <p>would be displaying stuff here</p>


    <div>
        <p><i>Start of Partial View</i></p>
        @Html.Partial("../RequestDetails/DetailsList", Model.RequestID)
        <p><i>End of Partial View</i></p>

        @Html.ActionLink("Edit", "Edit", new { id = Model.RequestID }) |
        @Html.ActionLink("Back to List", "MemberList")
    </div>
}

没有任何细节的页面可以完美地加载,就像这样

enter image description here

但是具有相应详细信息的页面将触发该块进入@HTML.Partial(),所有页面都会抛出此难看的错误:

The model item passed into the dictionary is of type 'System.Int16', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[CollectionMGR.Models.RequestDetail]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Int16', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[CollectionMGR.Models.RequestDetail]'.

Source Error: 

Line 85:     <div>
Line 86:         <p><i>Start of Partial View</i></p>
Line 87:         @Html.Partial("../RequestDetails/DetailsList", Model.RequestID)
Line 88:         <p><i>End of Partial View</i></p>

Line 89: 

Stack Trace: 


[InvalidOperationException: The model item passed into the dictionary is of type 'System.Int16', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[CollectionMGR.Models.RequestDetail]'.]
   System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +175
   System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +107
   System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +49

我很困惑!我可以整天访问DetailsList端点,但是当我尝试将其部分呈现为Partial视图时,它崩溃了吗?哇!

这是RequestDetails/DetailsList端点的代码:

@model IEnumerable<CollectionMGR.Models.RequestDetail>

@{
    ViewBag.Title = "_RequestDetails";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.RequestID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HostName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ResourceID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Request.User)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RequestID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.HostName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ResourceID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Request.User)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.RequestDetailsID }) |
                @Html.ActionLink("Details", "Details", new { id = item.RequestDetailsID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.RequestDetailsID }) |
                @Html.ActionLink("View Request", "../Request/Details/", new { id = item.RequestID })
            </td>
        </tr>
    }

</table>

我确信堆栈跟踪是 trying 会有所帮助,但是我不知道谈论类型转换的含义。当我手动导航到该视图时,我尝试进行部分渲染的视图可以完美地工作,因此我不确定为什么它会失败。

感谢任何指导,我已经在其他示例中浏览了相似的主题,只是没有理解。

1 个答案:

答案 0 :(得分:1)

该错误不言自明。您的DetailsList视图的类型为IEnumerable<CollectionMGR.Models.RequestDetail>。因此,它期望将RequestDetail个对象的集合传递给它。但是在您的代码中,您正在使用Html.Partial方法调用此视图,并将RequestID属性传递给它,该属性是一个int值。之所以得到该错误,是因为您传递的东西与预期的有所不同!

查看您的代码,我看到您有一个操作方法DetailsList,该方法获取DetailsList视图所需的数据并将其传递给该视图。因此,在您的主视图中,应该调用Html.Partial帮助方法,而不是调用Html.Action方法。

<p><i>Start of Partial View(Actually the Action method) </i></p>

@Html.Action("DetailsList",Model.RequestID)

<p><i>End of Partial View</i></p>

当razor执行主视图的代码时,它将执行此子操作DetailsList并将其结果添加到主视图输出中。