6

I used the following code in one of my controller;

            if (Request.IsAjaxRequest()) {
            return RedirectToAction("PreviewAndSendEmail");
        }

I debugged it and it comes to return line and but redirect didn't occur. Is it possible to do that inside Ajax.BeginForm ? Here is the razor code;

    using(Ajax.BeginForm( new AjaxOptions { LoadingElementId = "loading" })) { 

    <b>Choose E-mail Template : </b>@Html.DropDownList("emailtemps")<br /><br />

    <input type="submit" value="Preview & Send" />

    <span id="loading" style="display: none;">
        <img title="loading..." alt="load" src="@Url.Content("~/Content/App_Icons/gifs/loading.gif")"
    </span>

}
tugberk
  • 54,046
  • 58
  • 232
  • 321

2 Answers2

15

You can't redirect in an AJAX action from the server. If you want your browser to redirect in an AJAX action you need to do it from javascript. Obviously using AJAX to redirect is absolutely useless. If you intend to redirect use a normal Html.Begin form and don't bother with AJAX.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • 1
    thanks for the comment. I agree with you on that. I used that in order to show the loading gif. is there any other way of showing the gif – tugberk Feb 18 '11 at 14:29
  • @Darin, I have an ajax function calling an action that updates an element with a form to edit an item of a list. that forms contains a submit button that executes another action (Save). I want that after the `Save` action is called, the main list should be updated with the new values of this item. Same thing with the delete button - after deleting an item, update list. – Shimmy Weitzhandler Apr 21 '13 at 01:43
  • @Darin Dimitrov "Obviously using AJAX to redirect is absolutely useless." What makes you think that ? It could be pretty usefull for avoiding to duplicate some code that exist in another action method. – Antoine Meltzheim Apr 06 '14 at 00:37
1

Why don't you call the PreviewAndSendMailEmail directly? Since you don't need the redirection itself (change in url,etc), there is no need to use it.

TDaver
  • 7,024
  • 5
  • 44
  • 91