代码之家  ›  专栏  ›  技术社区  ›  FoxDeploy

当直接访问时,可以查看partialview,但不能使用html.partiaul

  •  0
  • FoxDeploy  · 技术社区  · 6 年前

    我有一个这样的局部视图,它应该给我一个请求的所有相关记录:

    public actionresult detailslist(short?身份证)
    {
    var requestdetails=db.requestdetails.where(i=>i.requestid==id);
    返回视图(requestDetails.toList());
    
    }
    < /代码> 
    
    

    当被访问时,它加载特定ID的所有记录(在本例中,请求编号24)

    我正在尝试将此嵌入为部分视图以显示在我的主请求页上,如:

    @model collectionmgr.models.request
    
    @ {
    viewBag.title=“集合创建请求详细信息”;
    }
    
    <H2>详细信息</H2>
    
    和DIV & GT;
    <h4>请求概述</h4>
    <hr/><p>您可以在此处查看有关您的请求的详细信息</p>
    <dl class=“dl horizontal”>
    DT & GT;
    @html.displaynamefor(model=>model.user)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.user)
    和/和DD & GT;
    
    DT & GT;
    @html.displaynamefor(model=>model.appname)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.appname)
    和/和DD & GT;
    
    DT & GT;
    @html.displaynamefor(model=>model.packagename)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.packagename)
    和/和DD & GT;
    
    DT & GT;
    @html.displaynamefor(model=>model.action)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.action)
    和/和DD & GT;
    
    DT & GT;
    @html.displaynamefor(model=>model.collectionid)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.collectionid)
    和/和DD & GT;
    
    DT & GT;
    @html.displaynamefor(model=>model.computers)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.computers)
    和/和DD & GT;
    
    DT & GT;
    @html.displaynamefor(model=>model.disposed)
    /lt;/dt & gt;
    
    DD & GT;
    @html.displayfor(model=>model.disposed)
    和/和DD & GT;
    
    &l/dl& gt;
    &L/DIV & GT;
    & P & GT;
    @html.actionlink(“编辑”,“编辑”,new id=model.requestid))。|
    @html.actionlink(“返回列表”,“索引”)。
    
    <h3>@viewbag.detailstatus</h3>
    
    @if(viewBag.detailStatus==“未找到详细记录”)
    {
    <P>没有要显示的内容</P>
    }
    其他的
    {
    <p>将在此处显示内容</p>
    
    
    和DIV & GT;
    <P><I>部分视图开始</I></P>
    @html.partial(“../requestdetails/detailslist”,model.requestid)
    <P><I>局部视图结束</I></P>
    
    @html.actionlink(“编辑”,“编辑”,new id=model.requestid))。|
    @html.actionlink(“返回列表”,“成员列表”)
    &L/DIV & GT;
    }
    < /代码> 
    
    

    没有任何详细信息的页面会完全加载,就像这样

    但是具有相应详细信息的页面,这将触发块到>@html.partial().all throw this丑陋的错误:.

    传递到字典中的模型项属于“System.Int16”类型,但此字典需要“System.Collections.Generic.IEnumerable”1[CollectionMgr.Models.RequestDetail]类型的模型项。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。 异常详细信息:System.InvalidOperationException:传递到字典的模型项的类型为“System.Int16”,但此字典需要“System.Collections.Generic.IEnumerable”1[CollectionMgr.Models.RequestDetail]类型的模型项。 源错误: 第85行:<DIV> 第86行:<P><I>部分视图开始</I></P> 第87行:@html.partial(“../requestdetails/detailslist”,model.requestid) 第88行:局部视图结束 第89行: 堆栈跟踪: [InvalidOperationException:传入字典的模型项的类型为“System.Int16”,但此字典需要“System.Collections.Generic.IEnumerable”1[CollectionMgr.Models.RequestDetail]类型的模型项。] system.web.mvc.viewDataDictionary`1.setModel(对象值)+175 system.web.mvc.viewdatadictionary..ctor(viewdatadictionary字典)+107 system.web.mvc.webviewpage`1.setviewdata(viewdataDictionary viewdata)+49 < /代码>

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

    以下是requestDetails/detailsList的代码endpoint:。

    @model ienumerable<collectionmgr.models.requestdetail>
    
    @ {
    viewBag.title=“请求详细信息”;
    }
    
    <table class=“table”>
    T>
    & TH & GT;
    @html.displayNamefor(model=>model.requestID)
    &T/TH>
    & TH & GT;
    @html.displaynamefor(model=>model.hostname)
    &T/TH>
    & TH & GT;
    @html.displayNamefor(model=>model.resourceID)
    &T/TH>
    & TH & GT;
    @html.displaynamefor(model=>model.request.user)
    &T/TH>
    <th></th>
    & LT/TR & GT;
    
    @foreach(模型中的var项)
    {
    T>
    TD & GT;
    @html.displayfor(modelitem=>item.requestid)
    &T/GT;
    TD & GT;
    @html.displayfor(modelitem=>item.hostname)
    &T/GT;
    TD & GT;
    @html.displayfor(modelitem=>item.resourceID)
    &T/GT;
    TD & GT;
    @html.displayfor(modelitem=>item.request.user)
    &T/GT;
    TD & GT;
    @html.actionlink(“编辑”,“编辑”,new id=item.requestDetailsid)|
    @html.actionLink(“详细信息”,“详细信息”,new id=item.requestDetailsID)|
    @html.actionLink(“删除”,“删除”,new id=item.requestDetailsid))。|
    @html.actionlink(“查看请求”,“../request/details/”,new id=item.request id)
    &T/GT;
    & LT/TR & GT;
    }
    
    &表;
    < /代码> 
    
    

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

    感谢您的指点,我已经浏览了其他类似主题的示例,但没有理解。

    当被访问时,它加载特定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端点,但是当我尝试将其部分呈现为局部视图时,它会崩溃吗?哇!

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

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

    感谢您的指点,我已经浏览了其他类似主题的示例,但没有理解。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Shyju    6 年前

    这个错误是不言而喻的。你的 DetailsList 视图强类型为 IEnumerable<CollectionMGR.Models.RequestDetail> . 所以它希望 RequestDetail 要传递给它的对象。但是在代码中,您使用 Html.Partial 方法和通过 RequestID 它的属性,它是 int 价值。您收到错误是因为传递的内容与预期的不同!

    看看你的代码,我发现你有一个动作方法 详细清单 ,它获取 详细清单 查看并传递到该视图。所以在主视图中,而不是调用 子页面 方法,您应该调用 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执行主视图的代码时,它将执行此子操作 详细清单 并将结果添加到主视图输出中。