gpt4 book ai didi

css - 使用 MVC 时的模型问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:22 33 4
gpt4 key购买 nike

我在 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 的新手,因为 iv 刚刚从 webforms 转移过来)。

我只想让它转到管理页面。我已经包含了我认为可以帮助人们调试此错误的文件中的代码。如果还有其他需要,请询问。

_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 &bull; <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(导航栏片段)

<!-- 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 &bull;
<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 -->

Controller

//
// 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);
}

最佳答案

您可以尝试将 IndexViewModel 用作 LoginViewModel 中的属性吗?

关于css - 使用 MVC 时的模型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27551950/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com