Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to action with JsonResult

Tags:

c#

asp.net-mvc

I have a view. When user clicks on a button in my view, I am trying to get some data from my view without reloading my view through an AJAX POST to display some data.

This is the method in my controller :

[HttpPost]
public JsonResult GetUserTraj()
{
    var userTrajList =
        DBManager.Instance.GetUserTraj(Session["UserName"].ToString());
    return Json(userTrajList);
}

This returns a Json Result. I am trying to implement session now. So if the session has expired, I want user to be redirected to the Login view. In the above case if the session expires Session["UserName"] becomes null and an exception is raised.

[HttpPost]
public JsonResult GetUserTraj()
{
    if (Session["UserName"] != null)
    {
        var userTrajList =
            DBManager.Instance.GetUserTraj(Session["UserName"].ToString());
        return Json(userTrajList);
    }
    else
    {
        RedirectToAction("Login", "Login");
        return Json(null);
    }
}

I tried the above code. But it doesn't work. It does not redirect the user to the Login view. I tried return RedirectToAction("Login", "Login");. But that doesn't work since the controller action method cannot return something other than JsonResult. Please help me find a solution for the same.

like image 589
ViVi Avatar asked Nov 22 '16 05:11

ViVi


People also ask

What does JsonResult return?

For example, returning JsonResult returns JSON-formatted data. Returning ContentResult or a string returns plain-text-formatted string data.

Which method of JsonResult is used to create JSON result?

Json(Object, String, Encoding, JsonRequestBehavior) Creates a JsonResult object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior.

What is RedirectToAction MVC?

RedirectToAction(String) Redirects to the specified action using the action name. RedirectToAction(String, Object) Redirects to the specified action using the action name and route values.


2 Answers

If you use AJAX to request a page, it's cant redirect in browser. You should response a status code, and then use javascript to redirect in front, like this

[HttpPost]
public JsonResult GetUserTraj()
{
    if (Session["UserName"] != null)
    {
        var userTrajList =
            DBManager.Instance.GetUserTraj(Session["UserName"].ToString());
        return Json(userTrajList);
    }
    else
    {
        //RedirectToAction("Login", "Login");
        return Json(new {code=1});
    }
}

You need write this condition Inside of your Ajax success call to reload login screen,

if(result.code ===1){
    window.location = 'yourloginpage.html'
}
like image 54
Rwing Avatar answered Oct 01 '22 10:10

Rwing


You can't redirect user to a new page using ajax. For this you have to send some flag at client side and then need to use that flag to identify that session has been expired. Following code will help you:

[HttpPost]
public JsonResult GetUserTraj()
{
    if (Session["UserName"] != null)
    {
        var userTrajList = DBManager.Instance.GetUserTraj(Session["UserName"].ToString());
        return Json(new { Success = true, Data = userTrajList});
    }
    else
    {
        return Json(new { Success = false, Message = "Session Expired"});
    }
}

jQuery

$.ajax({
  url: "any url",
  dataType: '',
  contentType: "------",
  success: function(response){
    if(response.Success){
     // do stuff
    }else{
    window.location.href = "/YourLoginURL.aspx"
    }
  }
});
like image 45
Ankush Jain Avatar answered Oct 01 '22 11:10

Ankush Jain