I tried using a RedirectToAction
after I have done a post to the controller and saved but the URL does not change and the redirect does not seem to work. I need to add that the redirect does enter the controller action method when I debug. It does not change the URL or navigate to the Index
view.
public ViewResult Index() { return View("Index", new TrainingViewModel()); } public ActionResult Edit() { // save the details and return to list return RedirectToAction("Index"); }
What am I doing wrong?
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{resource}.js/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); }
jQuery calls:
this.SynchValuesToReadOnly = function() { window.location = $('#siteRoot').attr('href') + 'Sites/'; }; this.OnSiteEdit = function() { // post back to the server and update the assessment details var options = { target: '', type: 'post', url: '/Site/Edit', beforeSubmit: othis.validate, success: othis.SynchValuesToReadOnly }; $('#uxSiteForm').ajaxSubmit(options); };
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
My Question - Can I pass student model in RedirectToAction? Since the route dictionary deals with objects, try changing the GetStudent action to accept an object and inside cast it to Student . Another option would be to serialize it using JSON when passing it from FillStudent .
To pass multiple values to the new controller method, set TempData values and/or pass them as parameters. First, add keyword/value pairs to the TempData collection to pass any number of values to the view. The temp data collection is persisted across controller method calls.
The code you show is correct. My wild guess is that you're not doing a standard POST (e.g., redirects don't work with an AJAX post).
The browser will ignore a redirect response to an AJAX POST. It's up to you to redirect in script if you need to redirect when an AJAX call returns a redirect response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With