0

I'm trying to redirect from my controller during the middle of an Ajax.Actionlink call. I have an Ajax.Actionlink like so:

@Ajax.ActionLink("Send a Project",
                 "RegisterAjax", "Projects",
                 new {id = Model.IncidentId},
                 new AjaxOptions {HttpMethod = "POST", UpdateTargetId = "Projectmsg_" + Model.IncidentId})

Then in my controller, I have the following:

public ActionResult RegisterAjax(int id = 0)
        {
            string result = RegisterProject(id);
            if (result != null)
            {
                return RedirectResult(result);
            }
            return Content("Sent...");
        }

private ActionResult RedirectResult(string result)
        {
            throw new NotImplementedException();
        }

If result != null, I've tried the above, return Redirect(result), return View(result), etc. and nothing gets me to the redirected result page (e.g. ~/Views/Manage/Location.cshtml). I've verified that the result is actually returning the path (by returning it in return Content(result)) . However, when I click on the hyperlink (i.e. the Ajax.Actionlink), it is completely unresponsive, but I've verified it's doing everything successfully in the background except that redirect.

Anyone know the trick? Many thanks.

Sum None
  • 2,164
  • 3
  • 27
  • 32
  • The whole point of ajax is that it stays to the same page. It does not redirect. –  Jul 01 '15 at 22:23
  • Yes, that is/was primarily the function. So, the parent page this is apart of is displaying a table/grid from a database and each row has a link (the Ajax.Actionlink). Before, when you click the link it will go do it's thing and then just send back the result to the table cell, which worked fine. Now, I'm trying to add more functionality to it by checking if the user's location is set and redirecting them to the set location page if it's not (before performing any of the things the ajax call actually does). But, seems like ajax is very limited in what it will return? – Sum None Jul 02 '15 at 06:25
  • Ajax is not limited in what it will return at all. You seem to be misunderstanding what ajax is. Its for staying on the same page (it will never redirect). If you want to redirect, do a normal submit, or return a value indicating the url you want to go to and the use `location.href='yourUrl'; –  Jul 02 '15 at 06:34
  • My basic understanding of Ajax is that it simply returns a limited set of data without refreshing the whole page...? However, if the data has to travel to my controller, seems like I should be able to circumvent the whole process somewhere along the way? I think I'm misunderstanding the client/server relationship in the process and what each one's role/behavior is for an ajax call? – Sum None Jul 02 '15 at 06:56
  • Yes, it _"returns data to the page"_ which means it does not redirect to another page. –  Jul 02 '15 at 07:01
  • Ok, thanks for your help. I might need to update my question then as I wouldn't know the best way to handle that on the client side either, my first thought seems like I would still need an if/else situation...but, maybe not... At first glance, it appears most people on SO just call a js function or similar based on their ajax result... – Sum None Jul 02 '15 at 08:00

2 Answers2

0

It sounds like you don't need AJAX, and you might be better off using a normal HTML form with a POST action. You should be able to redirect within the Action that handles the POSTing. Is there any reason you believe you need AJAX in this scenario?

Ron Brogan
  • 892
  • 1
  • 9
  • 25
  • Hi, please see my reply above, which might make more sense why a form wouldn't necessarily be the best choice. Thanks. – Sum None Jul 02 '15 at 06:26
0

Based on the comments above with Stephen, it appears what I'm trying to do is not possible (as I previously imagined it). So, my "not wanting to spend several more days on this" solution was based on Darin's answer here:

Getting JSonResult from ASP's Ajax.ActionLink

I basically changed my Ajax.Actionlink to an Html.Actionlink (which I did because Darin recommends it, but also because I could then use my Redirect in the controller, despite not using it there after all):

    @Html.ActionLink("Send a Project", "RegisterAjax", "Projects", 
            new { Id = Model.IncidentId }, 
            new { id = "sendaproject_" + Model.IncidentId })

I removed the if statement in my controller (because it stopped working when I implemented js) and changed the return result to json (mainly because I wasn't having any luck getting the return Content data into js):

return Json(new { jsonResult = result }, JsonRequestBehavior.AllowGet);

Then, I added the following javascript to deal with the behavior I wanted:

    <script type="text/javascript">
    $(function() {
        $('#sendaproject_@Model.IncidentId').click(function() {
        $.post(this.href, function(json) {
            if (json.jsonResult === null) {
                $("#projectmsg_@Model.IncidentId").html("Sent...");
            } else {
                window.location.href = json.jsonResult;
            }
            //alert(json.jsonResult);
        });
        return false;
    });
});
</script>

Here are my issues with this solution:

  1. I don't really understand why my redirect in the controller stopped working as soon as I started implementing the javascript. Seems cleaner to have the redirect in the controller...?
  2. The javascript is directly after each Html.Actionlink div because I can't think how to clean this up as each block has a unique id. I'm guessing I need to name my js function and pass the id to it?
  3. return Content seems like it would be less data sent back to the client since I'm only using one piece of data...?

The behavior works as wanted/intended now. But, since I don't really know what I'm doing yet, and since I don't really understand the complete chain of events from client to server (i.e. behaviors) I'm hoping I'll come back to this at some point and know better what to do as well as clean it up a bit...feel free to comment. I'm not insecure or easily offended or trying to be the smartest guy on this site, so feel free to shred. Thanks.

Community
  • 1
  • 1
Sum None
  • 2,164
  • 3
  • 27
  • 32