MVC页面跳转的问题,各位看看

gouhan02 2012-09-06 10:33:35
现象是这样的,我用了两种方式来触发Action操作。
1,我在view里采用的是submit方式,来提交表单的数据,触发controller下的Action.这样可以使用RedirectToAction实现页面跳转。controller代码如下:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(string textUserCode, string textPassword, string textPasswordConfirm, string textName, string textBirthday, string textArea,
string textJob, string textPhone, string textEmail, string textMemo)
{
Business.User user = new User();
if (string.IsNullOrEmpty(textUserCode))
{
ModelState.AddModelError("textUserCode", "请输入用户名!");
}
else
{
if (Encoding.Default.GetByteCount(textUserCode) > 50)
{
ModelState.AddModelError("textUserCode", "长度不能超过50!");
}
else
{
bool result = user.IsUserExist(textUserCode);
if (result)
{
ModelState.AddModelError("textUserCode", "用户名已存在!");
}
}
}
if (textPassword.Length < 6 || textPassword.Length > 10)
{
ModelState.AddModelError("textPassword", "密码必须在6-10位之间!");
}
else
{
if (textPassword != textPasswordConfirm)
ModelState.AddModelError("textPasswordConfirm", "两次密码输入不一致!");
}
//个人资料栏的验证
if (Encoding.Default.GetByteCount(textName) > 50)
{
ModelState.AddModelError("textName", "长度不能超过50!");
}
if (Encoding.Default.GetByteCount(textArea) > 50)
{
ModelState.AddModelError("textArea", "长度不能超过50!");
}
if (Encoding.Default.GetByteCount(textJob) > 50)
{
ModelState.AddModelError("textJob", "长度不能超过50!");
}
if (Encoding.Default.GetByteCount(textEmail) > 50)
{
ModelState.AddModelError("textEmail", "长度不能超过50!");
}
if (Encoding.Default.GetByteCount(textMemo) > 50)
{
ModelState.AddModelError("textMemo", "长度不能超过200!");
}
if (!ModelState.IsValid)
return View();
//ModelState.AddModelError("textUserCode", "用户名已存在!");
//ModelState.AddModelError("textPassword", "密码必须在6-10位之间!");
Hashtable htUser = new Hashtable();
htUser["USER_CODE"] = textUserCode;
htUser["USER_PWD"] = SecurityUtil.Encrypt(textPassword, textUserCode);
htUser["USER_NAME"] = textName;
htUser["USER_MOTHER_LAND"] = textArea;
htUser["USER_WORK"] = textJob;
if (!string.IsNullOrEmpty(textBirthday.TrimStart().TrimEnd()))
htUser["USER_BIRTHDAY"] = textBirthday;
htUser["USER_PHONE"] = textPhone;
htUser["USER_EMAIL"] = textEmail;
htUser["USER_MEMO"] = textMemo;
htUser["UPDATE_DATE"] = htUser["CREATE_DATE"] = DateTime.Now;
if (user.InsertUser(htUser))
{
//Response.Write("<javascript>alert('注册成功!');</javascript>");
return RedirectToAction("Login");//这里可以跳转成功
}
else
return View();
}

2在登录Login的view里面,没有采用表单提交的方式,而是自己写ajax方法来发出异步的请求操作,就不能用上面的RedirectToAction方式跳转到登录成功之后的页面。
ajax方法代码:

$(document).ready(function() {
$("#imgCheckCode").attr("src", "GetCheckPage.aspx?tempid=" + new Date().getTime()); //显示验证码
$("#chckPassword").check();
var m = $.Max(2, 3);
//提交
$("#buttonLogin").click(function() {
$.ajax({
data: { "userName": $("#textUser").val(), "password": $("#textPassword").val(), "checkCode": $("#textCheckCode").val() },
dataType: "json",
url: "CheckLogin.aspx",--这里是登录验证的请求url
success: function(data, textStatus) {
if (data == -1) {
alert("验证码输入有误!");
}
else if (data == 0) {
alert("用户名不存在或密码错误!");
}
else if (data == 1) {
window.location = "Home.aspx";
}
}
});
});
$("#buttonRegister").click(function() {
window.location = "Register.aspx";
});
});


CheckLogin.aspx对应的controller下的Action代码:

public ActionResult CheckLogin()
{
//result -1:验证码错误 0:用户密码验证失败 1:成功
int result = (Request.Params["checkCode"].ToString() == GlobalVariable.GlobalSession["CheckCode"].ToString()) ? 0 : -1;
if (result == -1)
return new JsonResult { Data = result };
Business.User user = new User();
result = user.CheckUser(Request.Params["userName"].ToString(), Request.Params["password"].ToString()) ? 1 : 0;
if (result == 1)
{
Session["IsLogin"] = 1;
return new JsonResult { Data = result };
}
return Redirect("/Home/HomePage");//我想在这里跳转到HomeController下的HomePage这个Action执行返回后的View。但是方法执行到了,就是url没有跳转。
//return RedirectToRoute("HomePage");
//return RedirectToAction("HomePage", "Home");
}

HomeController下的HomePage代码段:

public ActionResult HomePage()
{
return View("Home");
}
...全文
7478 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_16679135 2014-07-03
  • 打赏
  • 举报
回复
Redirect("/Home/HomePage"); 这里很明显,你两边写反了,前面应该写跳转页面,后面写控制器,如:Redirect("/HomePage/Home");
钟钟 2013-05-23
  • 打赏
  • 举报
回复
代码里面的这个Encoding是怎么出来的啊?我写的为啥出不来这个啊
zhujiazhi 2012-09-06
  • 打赏
  • 举报
回复
用ajax想怎么跳的,如果用ajax的话,后台只能处理一些东西的,跳转由前台控制的
gouhan02 2012-09-06
  • 打赏
  • 举报
回复
Sorry,I find the answer on another bbs.The reason some one explores as below:
The code you show is correct. My wild guess is that you're not doing a standard POST (e.g., redirects don't work with an AJAX post).

The browser will ignore a redirect response to an AJAX POST. It's up to you to redirect in script if you need to redirect when an AJAX call returns a redirect response.
.
gouhan02 2012-09-06
  • 打赏
  • 举报
回复
补充:想通过Redirect("/Home/HomePage");跳转到目标页面,但是Action方法是执行了,就是页面没有发生跳转。找了很久,也没能搞定,不只到哪里写的有问题,希望大家看看,是否能帮忙解决。
谢谢
WIKESOFT 2012-09-06
  • 打赏
  • 举报
回复
AJAx提交 没有办法 实现页面跳转的
blandness 2012-09-06
  • 打赏
  • 举报
回复
MVC AJAX 后必须有一个返回值到前台 然后前台控制跳转 不要纠结这问题了 我研究很久了

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧