【问题标题】:Error render PartialView "The model item passed into the dictionary is of type"错误渲染 PartialView “传入字典的模型项是类型”
【发布时间】:2015-02-02 13:10:57
【问题描述】:

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

@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'。

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

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:

    您的_LoginPartial 需要LoginModel 类型的模型:

    @model LPDPWebApp.Models.Access.LoginModel
    

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

    @Html.Partial("_LoginPartial")
    

    您没有指定任何模型,因此使用父模型 (IEnumerable&lt;LPDPWebApp.User&gt;) 并抛出异常,因为它们不匹配。

    试试这个:

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

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

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

    【讨论】:

    • 哇,它的工作原理@Html.Partial("_LoginPartial", new LoginModel());谢谢老兄
    • @yozawiratama:虽然这个答案是正确的,并且解决了表面问题,但真正的问题是你不应该首先这样做。如果您想保护视图,您应该将[Authorize] 属性添加到您的操作中。然后,除非用户登录,否则用户甚至不会进入此处的视图,如果没有,他们将自动重定向到您的登录页面。
    • @ChrisPratt 感谢您的建议,您能告诉我在哪里可以找到使用 [Authorize] 学习的最简单方法吗?
    • 转到MVC site 并按照教程进行操作 - 有一个关于身份验证和授权的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2013-05-04
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多