asp.net mvc ajax post - redirecttoaction not working
Asked Answered
P

2

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>

}
Pox answered 18/2, 2011 at 13:33 Comment(1)
Possibly duplicate of #3535456Kana
S
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.

Seclusion answered 18/2, 2011 at 13:37 Comment(3)
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 gifPox
@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.Riba
@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.Femme
P
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.

Pebrook answered 18/2, 2011 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.