4

I'm trying to redirect a user from a method on a controller to another view but can't get it to work no matter what I do. What am I doing wrong? Here's my code:

            public ActionResult SubmitReport(string JsonStringSend)
        {
            dynamic JSend = JObject.Parse(JsonStringSend);
            var schema = JsonSchema4.FromType<ReportItem>();
            var schemaData = schema.ToJson();
            var errors = schema.Validate(JSend.JsonString);
            schema = JsonSchema4.FromJson(schemaData);


            //Check for errors and show them if they exist
            if (errors.Count > 0)
            {
                //JSchema schema = JSchema.Parse(schema);
                foreach (var error in errors)
                    Console.WriteLine(error.Path + ": " + error.Kind);

                //JObject JsonString = JObject.Parse(JsonObj.JsonString.ToString());
                //JObject JsonStringSent = JObject.Parse(JsonStringSend);

            }
            else
            {
                return Redirect("/Admin/Reporting/ReportManagement");
            }
            return View();
        }

It never redirects. I've even tried these:

Response.Redirect(Url.Action("/ReportManagement"));
RedirectToRoute(new { contoller = "ReportManagement", action = "Reporting" });
return RedirectToRoute(new { contoller = "Reporting", action = "ReportManagement" });
return RedirectToAction("ReportManagement");

Nothing seems to redirect, what gives?

Jeff Mcbride
  • 453
  • 2
  • 6
  • 19
  • If you step through the code in debug mode, what happens? – Cortright May 06 '16 at 20:26
  • Nothing, when I get to `return Redirect("/Admin/Reporting/ReportManagement");` it just keeps on stepping – Jeff Mcbride May 06 '16 at 20:37
  • What's your current URL? – Cortright May 06 '16 at 20:46
  • https://plus.inroll.com/Admin/Reporting/EditReport – Jeff Mcbride May 06 '16 at 20:46
  • the return RedirectToAction("Whatever") should respond to your browser with a 302 response and make the browser request the "Whatever" URL. That's really strange. You could probably fire up Fiddler and see what response is coming back, if any. – Cortright May 06 '16 at 20:50
  • Maybe it's due to the ajax call to the method? Don't know why, but this is weird. – Jeff Mcbride May 06 '16 at 20:52
  • I think it's because of the AJAX. Put the redirect in the success() method. `$.ajax({ url: url, type: 'POST', success: function (d) { window.location.href = redirectUrl; } });` – Eric May 06 '16 at 21:55
  • I can't do that because it it will always success, I'm doing logic in the controller and if true, redirect and if false, display modal. – Jeff Mcbride May 06 '16 at 22:19
  • Then, return a value from the `SubmitReport` action that can be evaluated on Ajax success: `function(d) { if (d.valid) { location.href = redirectUrl; } else { //invalid flow } }` – Balde May 06 '16 at 22:55

2 Answers2

16

You don't redirect to a view, you redirect to an action or a route. If I'm correctly parsing the path you're attempting, where you have this:

return Redirect("/Admin/Reporting/ReportManagement")

should be

return RedirectToAction("Reporting", "ReportManagement", new { area="Admin" }) 

This assumes a Reporting action on a ReportManagementController class in the Admin area.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
4

Try this, it should be work:

return RedirectToAction("yourAnotherActionName","yourAnotherControllerName"); //It's very simply;)

I've tested it!

Husni Salax
  • 1,968
  • 1
  • 19
  • 29