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

视图未显示

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.

    评论

报告相同问题?