Iv在mvc和导航栏中创建了一个网站,我更改了允许MVC显示登录按钮(如果用户未登录)或其名称(如果是)的代码。
当用户点击登录时,会显示一个弹出窗口以允许他们登录,这样一旦用户登录用户名就会显示正常。
当我点击导航栏中的用户名时出现问题。它应该带我到管理索引页面,但反过来给我错误:
Additional information: The model item passed into the dictionary is of type
'Website.Models.IndexViewModel', but this dictionary requires a model item of type
'Website.Models.LoginViewModel'.
我不能为我的生活弄清楚什么是错的(我是MVC的新手,因为我刚刚从网络表格中移除)。
我只是希望它转到管理页面。我已经包含了文件中的代码,我认为这将有助于人们调试此错误。如果有其他需要,请问问。
_LoginPartial.cshtml
@using CoffeePixel.Models
@using Microsoft.AspNet.Identity
@model LoginViewModel
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
<!-- add a clopasable pill type thing here to allow users to navigate the members area-->
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<style>
#topHead .signin-dd form button:after {
border: none !important;
}
</style>
<!-- SIGN IN -->
<div class="pull-right nav signin-dd">
<a id="quick_sign_in" href="page-signin.html" data-toggle="dropdown"><i class="fa fa-users"></i><span class="hidden-xs"> Sign In</span></a>
<div class="dropdown-menu" role="menu" aria-labelledby="quick_sign_in" style="-moz-min-width: 200px; -ms-min-width: 200px; -o-min-width: 200px; -webkit-min-width: 200px; min-width: 200px;">
<h4 class="text-center">Sign In</h4>
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Use a local account to log in.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-12">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Username or Email" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="input-group">
<!-- password -->
@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
<!-- submit button -->
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">Sign In</button>
</span>
</div>
<div class="checkbox">
<!-- remmember -->
<label>
@Html.CheckBoxFor(m => m.RememberMe) Remember me • <a href="@Url.Action("ForgotPassword", "Account")">Forgot password?</a>
</label>
</div>
}
<hr />
<section id="socialLoginForm">
@Html.Partial("~/Views/Account/_ExternalLoginsListPartial.cshtml", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
</section>
<p class="bottom-create-account">
<a href="@Url.Action("Register", "Account")">Not Registered?</a>
</p>
</div>
</div>
<!-- /SIGN IN -->
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Layout.cshtml(navbar代码段)
<!-- Top Bar -->
<header id="topHead">
<div class="container">
<!-- PHONE/EMAIL -->
<span class="quick-contact pull-left">
<i class="fa fa-phone"></i> 1800-555-1234 •
<a class="hidden-xs" href="mailto:mail@yourdomain.com">mail@domain.com</a>
</span>
<!-- /PHONE/EMAIL -->
<!-- LANGUAGE -->
<div class="btn-group pull-right hidden-xs">
<button class="dropdown-toggle language" type="button" data-toggle="dropdown">
<img src="/Assets/images/flags/us.png" width="16" height="11" alt="EN Language" /> English <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="#">
<img src="/Assets/images/flags/us.png" width="16" height="11" alt="EN Language" /> [US] English
</a>
</li>
<li>
<a href="#">
<img src="/Assets/images/flags/de.png" width="16" height="11" alt="DE Language" /> [DE] German
</a>
</li>
<li>
<a href="#">
<img src="/Assets/images/flags/fr.png" width="16" height="11" alt="FR Language" /> [FR] French
</a>
</li>
<li>
<a href="#">
<img src="/Assets/images/flags/ru.png" width="16" height="11" alt="RU Language" /> [RU] Russian
</a>
</li>
</ul>
</div>
<!-- /LANGUAGE -->
<!-- CART MOBILE BUTTON -->
<a class="pull-right" id="btn-mobile-quick-cart" href="shop-cart.html"><i class="fa fa-shopping-cart"></i></a>
<!-- CART MOBILE BUTTON -->
<!-- LINKS -->
@Html.Partial("_LoginPartial")
<div class="pull-right nav hidden-xs">
<a href="@Url.Action("About", "Home")"><i class="fa fa-angle-right"></i> About</a>
<a href="@Url.Action("Contact", "Home")"><i class="fa fa-angle-right"></i> Contact</a>
</div>
<!-- /LINKS -->
</div>
</header>
<!-- /Top Bar -->
控制器
//
// GET: /Manage/Index
public async Task<ActionResult> Index(ManageMessageId? message)
{
ViewBag.StatusMessage =
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
: message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
: message == ManageMessageId.Error ? "An error has occurred."
: message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
: message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
: "";
var model = new IndexViewModel
{
HasPassword = HasPassword(),
PhoneNumber = await UserManager.GetPhoneNumberAsync(User.Identity.GetUserId()),
TwoFactor = await UserManager.GetTwoFactorEnabledAsync(User.Identity.GetUserId()),
Logins = await UserManager.GetLoginsAsync(User.Identity.GetUserId()),
BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(User.Identity.GetUserId())
};
return View(model);
}
答案 0 :(得分:1)
您可以尝试将IndexViewModel用作LoginViewModel中的属性吗?
答案 1 :(得分:0)
问题主要出现在第二种观点中。您应该确保manage index页面的模型是CoffeePixel.Models.IndexViewModel类型。
我假设您位于页面顶部:
@model LoginViewModel
如果是,请将其替换为:
@model IndexViewModel