weixin_33717298 2018-03-07 09:53 采纳率: 0%
浏览 25

视图未显示

I called ActionResult from javascript as below:

function checkSessionNewContribute(){
    if (@MountainBooks.Common.ProjectSession.UserID != 0) {
        window.location.href = "../Book/ContributeNewBook"
    }
    else{
        $.ajax({
            url: "@Url.Action("redirectToLogin", "Home")"
        });
    }
}

I need to pass TempData from this ActionResult:

 public ActionResult redirectToLogin(){
    TempData["Login"] = Resource.Messages.LoginRequired;
    return RedirectToAction("Index","Login");
 }

This is my Index Action:

public ActionResult Index() {
      string email = null;
      string password = null;
      if (Request.Cookies["Login"] != null){
        email = Request.Cookies["Login"].Values["LogedEmail"];
        password = General.DecryptCipherTextToPlainText(Request.Cookies["Login"].Values["LogedPwd"]);
      }

    ViewBag.ErrorMessage = TempData["wrongPassword"];
    ViewBag.Login = TempData["Login"];
    UserModel userModel = new UserModel();
    userModel.Email = email;
    userModel.Password = password;
    ViewData["LoginModel"] = userModel;
    userModel = new UserModel();
    userModel.Occupations = (_occupationService.GetAllOccupation());
    ViewData["RegisterModel"] = userModel;

    return View();

}

Index action is getting called, debugger also goes into the .cshtml file which Index Action returns as View(), but the page is not getting redirected. Is it because I used $.ajax() ? Is there another way to do this?

  • 写回答

1条回答 默认 最新

  • weixin_33691817 2018-03-07 10:02
    关注

    Inside else you can use window.location.href as follows,

    function checkSessionNewContribute()
    {
        if (@MountainBooks.Common.ProjectSession.UserID != 0) {
            window.location.href = "/Book/ContributeNewBook/"
        }
        else {
            window.location.href = "/Home/redirectToLogin/";
            //or
            //var url = '@Url.Action("redirectToLogin", "Home")';
            //window.location.href = url;
        }
    }
    

    The ajax() method is used to perform an AJAX (asynchronous HTTP) request. This method is mostly used for making requests to server. It is not possible to redirect to another page by ajax call.

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名