简体   繁体   中英

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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