在导航栏菜单中,我正在创建一个包含LoginPartial的下拉菜单。
这是LoginPartial代码:
<li class="nav-item dropdown">
<a href="#" id="LoginDropdownLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" role="button" class="nav-link dropdown-toggle fa fa-user-circle"></a>
<div class="dropdown-menu" aria-labelledby="LoginDropdownLink">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = "../../Home/Account" }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.TextBoxFor(m => m.Email, new { @class = "input-text", @placeholder = "Email Address" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
@Html.PasswordFor(m => m.Password, new { @class = "input-text", @placeholder = "Password" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
<input type="submit" value="Log in" class="btn btn-default" />
}
</div>
这就是我添加它的方式:
<ul class="nav navbar-nav navbar-right">
<li><a class="active" href="../../Home/Index">Home</a></li>
<li><a href="../../Home/Build">Build A Box</a></li>
<li><a href="#ContactUs">Contact Us</a></li>
@Html.Action("LoginPartial", "Account")
</ul>
这是控制器代码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
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
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
if (model.OrderId != null)
{
Orders order = db.Orders.Where(x => x.OrderId == model.OrderId).First();
order.UserId = User.Identity.GetUserId();
db.SaveChanges();
return RedirectToAction("Checkout1", "Home", new { model.OrderId });
}
else
{
return RedirectToAction("Index", "Account");
}
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);
}
}
我在var result=...
行上放置了一个断点,发现结果是“成功”。但是,我没有加载“帐户/索引”页面,而是重定向到了登录页面(带有返回URL):
为了完整起见,我包括了“帐户/索引”控制器代码。我临时向其添加了一个[AllowAnonymous]属性,以查看会发生什么,并且确实进行了加载(但是该模型当然不包含任何数据)。
public ActionResult Index()
{
AccountIndexViewModel model = new AccountIndexViewModel();
var Current = User.Identity.GetUserId();
model.Orders = db.Orders.Where(x => x.UserId == Current).OrderByDescending(x=> x.Date).ToList();
List<string> OrderIds = db.Orders.Where(x => x.UserId == Current).Select(x => x.OrderId).ToList();
model.OrderDetails = db.OrderDetail.Where(x=> OrderIds.Contains(x.OrderId)).ToList();
return View(model);
}