3

So, I have a small problem and I would like to know if there is any possibility of solving it in the following manner, I know there are other ways to go about the issue but I want to know if it could be possible to solve it like this:

I have an ajax call, a GET. On this ajax call I just want to pass some parameters based on user input (through javascript) and have the controller-action handle the rest. The problem is that, with ajax, the success function takes care of whatever happens after the action has completed.

I have a redirect on my action (ActionResult which returns a View with some parameters), I want to allow the controller to redirect me here, but ajax kicks in with the Success function.

How can I pass the full path (which can change since it is based on user input) to the success function in order to redirect?

Thank you!

 public string Edit(string stringEventID)
    {
        Guid eventID = new Guid(stringEventID);

        if (eventID == Guid.Empty || eventID == null)
        {
            return Url.Action("Edit", "MyController", new { area = "MyArea", id = new Event() });
        }

        else
        {
            Event _event = _iEMARepository.Events.Where(p => p.EventID == eventID).SingleOrDefault();

            return Url.Action("Edit", "MyController", new { area = "MyArea", id = _event })
        }
    }
1
  • My problem is not whether the request succeeded or not, I want the controller to redirect, regardless. But the url won't be the same, it'll depend on user input. How can I return the full path and redirect through window.location.href after that
    – Jose
    Aug 20, 2013 at 18:32

1 Answer 1

4

Use the Url helper to construction the URL properly for MVC and your routes. Then return a Jsonresult with the URL (you may need to set AllowGet):

string url = Url.Action(your arguments here);

return new JsonResult()
{
    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
    Data = new Dictionary<string,string>() { { "url", url } }
};

Than use the url in success function to trigger a redirect:

success: function (data) {
    window.location.href = JSON.parse(data)['url'];
}
14
  • Returning a string would definitely do the trick. I'm just wondering how javascript handles URLs, since I know it's different from how MVC handles them.
    – Jose
    Aug 20, 2013 at 18:33
  • I'm just wondering if I can "skip" the success function in this case, or not even use ajax at all (I believe this last one won't work since I am basing the redirect on user input and thus, need to send the parameter to the controller)
    – Jose
    Aug 20, 2013 at 18:34
  • 1
    Correct, you'll definitely want to call to the server in order to construct the URL. You will want to actually use the success function because you probably won't want to redirect if there is an error on the server or a problem with the ajax call. Aug 20, 2013 at 18:39
  • Right, I'm sorry if this seems like a dumb question, but would I have to construct the string from scratch then? As in with StringBuilder so I can add a {0} at the end (the parameter)?
    – Jose
    Aug 20, 2013 at 18:41
  • 1
    Yep I believe that is what you want to do. One action for the ajax call to return the URL and another action that will be invoked by that URL. Aug 20, 2013 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.