yozawiratama
yozawiratama

Reputation: 4318

Error render PartialView "The model item passed into the dictionary is of type"

I want to render login page if user session is over, this is my code :

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

but i get error on @Html.Partial("_LoginPartial") :

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[LPDPWebApp.User]', but this dictionary requires a model item of type 'LPDPWebApp.Models.Access.LoginModel'.

I just want to render _LoginPartial if Session is over, how to solve this problem?

Upvotes: 0

Views: 3730

Answers (1)

haim770
haim770

Reputation: 49095

Your _LoginPartial is expecting a model of type LoginModel:

@model LPDPWebApp.Models.Access.LoginModel

But when you're calling the @Html.Partial method:

@Html.Partial("_LoginPartial")

You don't specify any model hence the parent model (IEnumerable<LPDPWebApp.User>) is used and the exception is thrown because they don't match.

Try this:

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

It's also seems like you don't really use the model in your _LoginPartial, then you can even pass a null model to it:

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

Upvotes: 6

Related Questions