错误呈现PartialView"传递到字典中的模型项具有类型"

yoz*_*ama 0 c# asp.net-mvc razor

如果用户会话结束,我想渲染登录页面,这是我的代码:

@model IEnumerable<LPDPWebApp.User>

@{
    Layout = "~/Views/Shared/_LayoutDashboard.cshtml";

    String datenow = DateTime.Now.ToShortDateString();

    Boolean IsAuthorized = false;
    if (Session["username"] != null)
    {
        IsAuthorized = true;
        Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
    }
    else
    {
        Layout = "~/Views/Shared/_LayoutLogin.cshtml";
    }
}

@if (IsAuthorized)
{
    <div class="side-b">
        <div class="container-fluid">
            <div class="row page-title">
                <div class="col-md-12">
                    <button type="button" class="pull-right btn-layout-modal" id="modal-open" data-toggle="modal" data-target="#layout-modal">
                        <i class="fa fa-th fa-fw"></i>
                    </button>
                    <h4>Dana Kegiatan Pendidikan</h4>
                    <p>@datenow</p>
                </div>
            </div>
            <div class="row grid-sortable">
                <a href="@Url.Action("Create", "Account")" class="btn btn-sm btn-primary">Create</a>
                <table class="table">
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Username)
                            </td>
                            <td>
                                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                                @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                                @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                            </td>
                        </tr>
                    }

                </table>

            </div>
        </div>
    </div>

}
else
{
    @Html.Partial("_LoginPartial")
}
Run Code Online (Sandbox Code Playgroud)

_LoginPartial.cshtml

@model LPDPWebApp.Models.Access.LoginModel

@using (Html.BeginForm("Login", "Access", FormMethod.Post, new { id = "formLogin", @class = "form-vertical" }))
{
    @Html.AntiForgeryToken()
    <div class="col-md-4">
        @Html.TextBoxFor(m => m.Username, new { placeholder = "username", @class = "form-control", id = "tbxUsername" })
    </div>
    <div class="col-md-4">
        @Html.PasswordFor(m => m.Password, new { placeholder = "password", @class = "form-control", id = "tbxPwd" })
    </div>
    <div class="col-md-8">
        <button type="submit" class="btn btn-primary pull-right btn-submit">submit</button>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

但我得到错误@Html.Partial("_LoginPartial"):

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [LPDPWebApp.User]',但此字典需要类型为"LPDPWebApp.Models.Access.LoginModel"的模型项.

我只想在Session结束时渲染_LoginPartial,如何解决这个问题?

hai*_*770 6

_LoginPartial期待一个类型的模型LoginModel:

@model LPDPWebApp.Models.Access.LoginModel
Run Code Online (Sandbox Code Playgroud)

但是当你调用这个@Html.Partial方法时:

@Html.Partial("_LoginPartial")
Run Code Online (Sandbox Code Playgroud)

您没有指定任何模型,因此使用了父模型(IEnumerable<LPDPWebApp.User>),并且由于它们不匹配而抛出异常.

试试这个:

@Html.Partial("_LoginPartial", new LoginModel());
Run Code Online (Sandbox Code Playgroud)

您似乎并没有真正使用该模型_LoginPartial,那么您甚至可以将null模型传递给它:

@Html.Partial("_LoginPartial", null);
Run Code Online (Sandbox Code Playgroud)