0
我要呈現的登錄頁面,如果用戶會話結束了,這是我的代碼:錯誤渲染PartialView「傳遞到字典的模型項的類型是」
@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")
:
傳遞到字典的模型產品型210'System.Collections.Generic.List`1 [LPDPWebApp.User]',但這個 字典需要一個 'LPDPWebApp.Models.Access.LoginModel'類型的模型項。
我只想呈現_LoginPartial如果會話結束,如何解決這個問題?
哇它的工作原理@ Html.Partial(「_ LoginPartial」,new LoginModel());謝謝兄弟 – yozawiratama 2014-12-04 15:34:25
@yozawiratama:雖然這個答案是正確的,並修復了膚淺的問題,但真正的問題是,你不應該這樣做,首先。如果你想保護視圖,你應該添加'[Authorize]'屬性到你的動作。然後,除非用戶登錄,否則用戶甚至不會訪問該視圖,如果不登錄,他們將自動重定向到您的登錄頁面。 – 2014-12-04 16:35:25
@ChrisPratt感謝您的建議,你能告訴我在哪裏可以找到最簡單的方法來學習使用[授權]? – yozawiratama 2014-12-04 23:04:45