[英]why my register form is working and my login form does not
i am using the default microsoft template for registration and login pages.我正在使用默认的微软模板进行注册和登录页面。 I merged both pages in one view.
我在一个视图中合并了两个页面。 When i am registering a new user the website is working and the index page is showing up but when i am trying to login it says that i am trying to pass a loginViewModel instead of ViewModel.
当我注册新用户时,网站正在运行并且索引页面正在显示,但是当我尝试登录时,它说我正在尝试传递 loginViewModel 而不是 ViewModel。 I cant understand why the error is showing up but i cant understand why is working for registration and not for login since i am using the same approach for both pages.
我不明白为什么会出现错误,但我不明白为什么是注册而不是登录,因为我对两个页面使用相同的方法。 Can anyone help me fix this?
谁能帮我解决这个问题?
Here is my code:这是我的代码:
Login view:登录视图:
@using my_app.Models
@model my_app.Models.ViewModel
<div class="split right">
<div class="hold-transition login-page img-bg-mine" background="~/imgs/dog3.jpg">
<div class="centeredright">
<div class="login-box">
<div class="login-logo">
<a href="../../index2.html"><b>Pet</b>Care</a>
</div>
<!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">Login to start your session</p>
@Html.Partial("_Login")
</div>
</div>
</div>
</div>
</div>
<div class="split left">
<div class="hold-transition login-page img-bg-mine" background="~/imgs/dog3.jpg">
<div class="centeredleft">
<div class="login-box">
<div class="login-logo">
<a href="../../index2.html"><b>Pet</b>Care</a>
</div>
<!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">Register to start your session</p>
@Html.Partial("_Register")
</div>
</div>
</div>
</div>
</div>
Controller:控制器:
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var model1 = new ViewModel();
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
ViewBag.RoleList = new SelectList(context.Roles.ToList(), "Id", "Name");
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
//After he signs in check his role and you will redirect him to the correct page
//User.IsInRole use this(returns true or false)
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
// return RedirectToAction("Index", "Home");
}
}
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// var role = dbContext.AspNetRoles.Find(Convert.ToInt32(model.Role));
//var role = new IdentityRole(model.Role.Name);
//I added this line to store the user and its roles in AspNetUserRoles table:
await UserManager.AddToRoleAsync(user.Id, model.Role);
//result = await UserManager.AddToRoleAsync(user.Id, model.Role);
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
//Assign Role to user Here
// await this.UserManager.AddToRoleAsync(user.Id, model.Role);
return RedirectToAction("Index", "Home");
}
IEnumerable<SelectListItem> items = dbContext.AspNetRoles.Select(c => new SelectListItem { Value = c.Id, Text = c.Name });
ViewBag.RoleList = items;
AddErrors(result);
}
// If we got this far, something failed, redisplay form
LoginViewModel mod = new LoginViewModel();
return View("Login");
}
Model:模型:
public class ViewModel
{
public LoginViewModel LoginModel { get; set; }
public RegisterViewModel RegisterModel { get; set; }
}
In your controller you are returning object model
which is of type LoginViewModel
which comes from method parameters.在您的控制器中,您将返回来自方法参数的
LoginViewModel
类型的对象model
。 You define that your view expects ViewModel
, hence the error.您定义您的视图需要
ViewModel
,因此出现错误。 You instanciate var model1 = new ViewModel();
你实例化
var model1 = new ViewModel();
but never make use of it.但永远不要使用它。 You ant to return that instead.
你蚂蚁来代替它。
if (!ModelState.IsValid) { **return View(model);** }
ase SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); **return View(model);** // return RedirectToAction("Index", "Home");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.