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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?