简体   繁体   English

MVC局部视图将无法加载

[英]MVC Partial View Will Not Load

I keep getting an error message along the lines of: 我一直收到以下错误消息:

..but this dictionary requires a model item of type.. ..但该词典需要类型的模型项。

My view I'm trying to put the partial view into is: 我试图将部分视图放入的视图是:

@using TheNovelMachine.Models
@model TheNovelMachine.Models.Account

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<img id="homeimage" src="~/Image/front-page-book.jpg" alt="The Novel Machine" />

<div id="text" class="col-lg-12">
    <h1>Profile</h1>
    <div class="col-lg-6 no-gutter">
        <h2>@Html.DisplayFor(model => model.Username)</h2>
        <h4>@Html.DisplayFor(model => model.FullName)</h4>
        <p>@Html.DisplayFor(model => model.Email)</p>
    </div>

    <div class="col-lg-6 pull-right text-right no-gutter">
        <img class="profilepic" src="~/Image/@(Html.DisplayFor(model => model.Username)).jpg" />
    </div>

    <div class="col-lg-12 no-gutter">
        <hr/>
        <h4>@Html.DisplayFor(model => model.Username)'s Novels</h4>
        @{
            Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml");
        }
    </div>

</div>

And the Partial View is: 部分视图是:

@using System.Web.DynamicData
@using TheNovelMachine.Models
@model IEnumerable<TheNovelMachine.Models.Novel>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Genre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Abstract)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Privacy)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AccountId)
        </th>
    </tr>

@foreach (var item in Model.Where(i => i.AccountId.ToString() == Url.RequestContext.RouteData.Values["id"].ToString())) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Abstract)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Privacy)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AccountId)
        </td>
    </tr>
}

</table>

Your partial view is expecting this model (you are declaring it at the top of you partial view) 您的部分视图需要这种模型(您在部分视图的顶部声明了它)

@model IEnumerable<TheNovelMachine.Models.Novel>

You should pass that model in this line: 您应该在这一行中传递该模型:

@{
        Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", Model.Novels);
}

That should work, assuming Model.Novels is of this type IEnumerable<TheNovelMachine.Models.Novel> or it can be converted to that type. 假设Model.Novels属于IEnumerable<TheNovelMachine.Models.Novel>类型,则可以正常工作,也可以将其转换为该类型。

Here you have an example with more details, but the idea is the same. 在这里,您有一个包含更多细节的示例,但是想法是相同的。 If you don't pass this as a parameter, then the partial view can't be constructed (that's why you are getting that error). 如果不将其作为参数传递,则无法构造局部视图(这就是为什么会出现该错误的原因)。

MSDN Documentation here for RenderPartial method. 有关RenderPartial方法的MSDN文档,请参见此处。

So even though there are other answers as to what you need to do, here is what is actually happening, so you know in the future. 因此,即使您需要执行的操作还有其他答案,这也是实际发生的事情,因此您将来会知道。

@Html.RenderPartial("_NovelProfile");

would be the same code as: 与以下代码相同:

@Html.RenderPartial("_NovelProfile", Model)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM