在控制器中使用 Switch Case 添加局部视图
Adding Partial View with Switch Case in Controller
我正在处理一个局部视图,该视图由我的登录和注册控制器函数中的 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);
}
您需要在此处return,因为您没有使用这些方法的结果:
switch (btnReturn)
{
case "Register":
return DashboardRegister(model1, Role, formCollection);
case "Login":
return DashboardLogin(model2, returnUrl);
}
我正在处理一个局部视图,该视图由我的登录和注册控制器函数中的 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);
}
您需要在此处return,因为您没有使用这些方法的结果:
switch (btnReturn)
{
case "Register":
return DashboardRegister(model1, Role, formCollection);
case "Login":
return DashboardLogin(model2, returnUrl);
}