错误呈现部分视图“传递到字典中的模型项是类型”

本文关键字:模型 类型 字典 现部 视图 错误 | 更新日期: 2023-09-27 18:33:04

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

@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")
}

_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>
}

但是我在@Html.Partial("_LoginPartial")上收到错误:

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

我只想渲染_LoginPartial如果会话结束,如何解决这个问题?

错误呈现部分视图“传递到字典中的模型项是类型”

您的_LoginPartial期待 LoginModel 类型的模型:

@model LPDPWebApp.Models.Access.LoginModel

但是,当您调用@Html.Partial方法时:

@Html.Partial("_LoginPartial")

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

试试这个:

@Html.Partial("_LoginPartial", new LoginModel());
似乎

你并没有真正在_LoginPartial中使用该模型,那么你甚至可以将一个null模型传递给它:

@Html.Partial("_LoginPartial", null);