我正在处理部分视图,该视图由我的控制器函数中的Switch case语句控制,用于登录和注册,但在成功登录后,它只刷新页面并且不会重定向登录控制器上使用的案例功能,我的问题是如何防止进入return View(model)
这是我的主控制器
[HttpPost]
public ActionResult Dashboard(RegisterModel model1, LogOnModel model2, string returnUrl, string btnReturn, string Role, FormCollection formCollection)
{
switch (btnReturn)
{
case "Register":
DashboardRegister(model1, Role, formCollection);
break;
case "Login":
DashboardLogin(model2, returnUrl);
break;
}
ViewBag.Roles = new SelectList(Roles.GetAllRoles().ToList());
DashboardRegisterLogin model = new DashboardRegisterLogin
{
RegisterModel = model1,
LogOnModel = model2
};
// If we got this far, something failed, redisplay form
return View(model);
}
注册控制器功能:
public ActionResult DashboardRegister(RegisterModel model1, string Role, FormCollection formCollection)
{
String name = formCollection["txtClientName"];
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model1.UserName = model1.Email, model1.Password, model1.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
Roles.AddUserToRole(model1.UserName, Role);
FormsAuthentication.SetAuthCookie(model1.UserName, false /* createPersistentCookie */);
if (Roles.IsUserInRole(model1.UserName, "Employer"))
{
return RedirectToAction("ClientCustomerDetails", "Customer");
}
else if (Roles.IsUserInRole(model1.UserName, "Worker"))
{
return RedirectToAction("WorkerInfo", "Worker");
}
else if (Roles.IsUserInRole(model1.UserName, "Administrator"))
{
return RedirectToAction("ClientDetails", "Client");
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
// If we got this far, something failed, redisplay form
return View(model1);
}
登录控制器功能:
[HttpPost]
public ActionResult DashboardLogin(LogOnModel model2, string returnUrl)
{
if (Membership.ValidateUser(model2.UserName, model2.Password))
{
FormsAuthentication.SetAuthCookie(model2.UserName, model2.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
if (Roles.IsUserInRole(model2.UserName, "Employer"))
{
return RedirectToAction("WorkerIndex", "Worker");
}
else if (Roles.IsUserInRole(model2.UserName, "Worker"))
{
return RedirectToAction("PositionIndex", "Position");
}
else if (Roles.IsUserInRole(model2.UserName, "Administrator"))
{
return RedirectToAction("ClientDetails", "Client");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
// If we got this far, something failed, redisplay form
return View(model2);
}
答案 0 :(得分:5)
您需要返回此处,因为您没有使用这些方法的结果:
switch (btnReturn)
{
case "Register":
return DashboardRegister(model1, Role, formCollection);
case "Login":
return DashboardLogin(model2, returnUrl);
}