如果用户会话结束,我想渲染登录页面,这是我的代码:
@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,如何解决这个问题?
答案 0 :(得分:6)
您的_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);