我有一个登录页面。验证后我重定向到所需的页面。它重定向但没有
查看外观,而不是显示上一个登录页面查看。
这是我的Javascript COde
function abc() {
var email = document.forms["MyForm"]["Email"].value;
var password = document.forms["MyForm"]["Password"].value;
if (email.length == 0 || password.length == 0) {
alert("Email and Password Field Required");
return false;
}
else {
$.ajax({
url: $("#MyForm").attr('action'),
type: $("#MyForm").attr('method'),
data: $("#MyForm").serialize(),
success: function (data) {
alert("Invalid Email or Password");
}
});
}
}
</script>
这是控制器 [HttpPost]
public ActionResult UserLogin(Models.UserModel selectedDocuments)
{
if (ModelState.IsValid)
{
long AdminID = IsValid(selectedDocuments.Email, selectedDocuments.Password);
if (AdminID != 0)
{
FormsAuthentication.SetAuthCookie(selectedDocuments.Email, false);
if (RoleID == 1)
{
Session["SystemAdmin"] = true;
Session["AdminID"] = AdminID;
return RedirectToAction("ClubInfo", "Admin");
}
if (RoleID == 2)
{
Session["ClubAdmin"] = true;
Session["AdminID"] = AdminID;
return RedirectToAction("ClubInfo", "ClubAdmin");
}
if (RoleID == 3)
{
Session["NewsAdmin"] = true;
Session["AdminID"] = AdminID;
return RedirectToAction("ClubInfo", "NewsAdmin");
}
}
else
{
ModelState.AddModelError("", "Login Data Is Incorrect.");
}
}
return Json(new { selectedDocuments = "Whatever you want to send" }, JsonRequestBehavior.AllowGet);
}
SomeBody帮助我如何成功地做到这一点。
答案 0 :(得分:0)
我是通过改变重定向来实现的。
返回Json(Url.Action(&#34; ClubInfo&#34;,&#34; Admin&#34;));
并在javascript中
$.ajax({
url: $("#MyForm").attr('action'),
type: $("#MyForm").attr('method'),
data: $("#MyForm").serialize(),
success: function (result) {
window.location.href = result;
},
error:function ()
{
alert("Invalid Email or password");
}
});
答案 1 :(得分:0)
当通过ajax发布表单时,调用视图将作为ajax调用的响应返回,用户将无法查看重定向的视图。
而不是reutrn RedirectToAction()
使用return Json
并从客户端重定向:
return Json(new
{
RedirectUrl= Url.Action("ClubInfo", "Admin")
}
,JsonRequestBehavior.AllowGet);
和ajax的成功功能:
success: function (data) {
window.location.href = data.RedirectUrl;
}