0

This post action is called from a ajax jquery dialog.

When I have a template selected it should run the RedirectToAction method but the UnitController and GetTemplateRootUnits action is never hit?

What do I wrong?

   [HttpPost]
    public ActionResult Open(int selectedTemplateId)
    {
     if (ModelState.IsValid)
     {

     return RedirectToAction("GetTemplateRootUnits", "Unit", new { TemplateId = selectedTemplateId });

     }
     else
     {
         return LoadOpenTemplates();
     }          
    }

my route table:

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }
            );

my target controller/action to call:

[HttpGet]
public JsonNetResult GetTemplateRootUnits(int templateId)
{
 IEnumerable<Unit> units = _unitDataProvider.GetTemplateRootUnits(templateId);
 return new JsonNetResult(new { data = units });
}

 function openTemplate(dlg, form) {
        $.ajax({
            url: $(form).attr('action'),
            type: 'POST',
            data: form.serialize(),
            success: function (response) {
                if (response.success) {
                    dlg.dialog("close");
                    $('#TreeDiv').empty();
                    loadUnits(response.data);
                }
                else {  // Reload the dialog with the form to show model/validation errors                    
                    dlg.html(response);
                }
            }
        });
    }
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 3
    Two things to check: firstly, is the modelstate actually valid? Secondly, if it is, what does your route table look like? – John H Aug 10 '12 at 14:34
  • yes it is valid else the code would not hid the RedirectToAction. I use the default route table of asp.net mvc 4. – Elisabeth Aug 10 '12 at 15:08
  • It always shows the same jquery dialog where I can choose the templates. I have updated my code. – Elisabeth Aug 10 '12 at 15:34
  • Have you used something like fiddler to see the actual HTTP request/responses that are being sent between the browser and server (in this case VS). Perhaps the issue is that the browser isn't doing what you expect it to do. – Colin Mackay Aug 10 '12 at 15:37
  • ah hell of a crap. Due to my service refactorings I added somehow the same ninject binding to my IUnitDataProvider 2 times which is causing an controller server error which I got when I tried the redirectToAction on another controller... :/ – Elisabeth Aug 10 '12 at 15:45

1 Answers1

1

RedirectToAction will not work when you use an AJAX POST see:

RedirectToAction not working

You need to use javascript to redirect in this case.

A couple of approaches:

How to manage a redirect request after a jQuery Ajax call

RedirectToAction not redirecting

Community
  • 1
  • 1
Mamayo
  • 41
  • 2