现在的位置: 首页 > 综合 > 正文

location.href在action中的链接

2013年12月07日 ⁄ 综合 ⁄ 共 2025字 ⁄ 字号 评论关闭

在controllers的 action 中,可以通过action的结果返回不同的页面等,以管理员的登录为例子:

Code:
  1. #region================管理员登陆==============   
  2.        [AcceptVerbs(HttpVerbs.Get)]   
  3.        public ActionResult A_Login()   
  4.        {   
  5.            return View();   
  6.        }   
  7.        [AcceptVerbs(HttpVerbs.Post)]   
  8.        [ValidateInput(false)]   
  9.        public ActionResult A_Login(string A_nickname, string A_pwd)   
  10.        {   
  11.            if ((string.IsNullOrEmpty(A_nickname)) || (string.IsNullOrEmpty(A_pwd)))   
  12.            {   
  13.                if (string.IsNullOrEmpty(A_nickname))   
  14.            {   
  15.                return Content("<script>alert('用户名不能为空');location.href='A_Login';</script>");   
  16.            }   
  17.            else if (string.IsNullOrEmpty(A_pwd))   
  18.            {   
  19.                return Content("<script>alert('密码不能为空');location.href='A_Login';</script>");   
  20.            }   
  21.            }   
  22.            else  
  23.            {   
  24.                A_nickname = this.Request.Form["A_nickname"].ToString();   
  25.                A_pwd = this.Request.Form["A_pwd"].ToString();   
  26.                List<AdminInfo> listAdmin = aifb.GetAdminInfo(A_nickname, A_pwd).ToList();   
  27.                if (listAdmin.Count > 0)   
  28.                {   
  29.                    string admin = listAdmin[0].A_Name;   
  30.                    Session["admin"] = admin;   
  31.                    return Content("<script>alert('登陆成功,欢迎管理员');location.href='../BackManage/BackDefault';</script>");   
  32.                   // Console.WriteLine("<script>alert('登陆成功,欢迎管理员!')</script>");   
  33.                    //return RedirectToAction("BackDefault", "BackManage");   
  34.                }   
  35.                else  
  36.                {   
  37.                    return Content("<script>alert('输入信息有误,登录失败!');location.href='A_Login';</script>");   
  38.                }   
  39.            }   
  40.   
  41.            return RedirectToAction("BackDefault","BackManage");   
  42.        }  
  43.        #endregion  

由上可以看出,如果在同一个controllers中要返回不同的view页面,直接用 .....location.href='A_Login'...即可,如果是要

返回不同controllers中的不同view页面,则需要添加相对物理地址,例如上面代码中,若是登录成功,则用

...location.href='../BackManage/BackDefault'....进行链接,这样浏览器就会根据提示的地址找到相应的页面。

抱歉!评论已关闭.