Return redirect or PartialView from one action

I have a problem with Ajax.BeginForm. I have a form with submit and a button that updates a partial view.

What I'm trying to do is that I can use the following return statements in the same Action.

// button activates this return PartialView("PartialViewName", data); 

and

 // submit activates this return Redirect(model.Url); 

Now the problem is that redirecting causes problems with Ajax.BeginForm

 <% using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { OnSuccess = "OnSuccess", UpdateTargetId = "Container", InsertionMode = InsertionMode.Replace }, new { id = "eventAjaxForm" })) { %> 

How can I determine the result of a PartialView or are we redirecting the user?

0
redirect asp.net-mvc-2 partial-views
Nov 15 '10 at 13:15
source share
3 answers

Usually, when you make an AJAX call for a controller action, any redirects are performed until the client receives a 200 status code or error. This way, the HTML of the URL you are redirecting will be placed in the Container div. On the other hand, in the action of your controller, you can check whether the call was asynchronous with Request.IsAjaxRequest() and return a partial one.

+2
Nov 15 '10 at 13:19
source share
— -

If you are using the Post Redirect Get template, you will need to use Request.IsAjaxRequest () in combination with TempData, i.e. you store the result of Request.IsAjaxRequest () in TempData, so when you get a redirected request, you can check TempData to see if it is the result of the original Ajax request.

This is well explained in Stephen Sanderson's book on Pro ASP.NET MVC 2.

+1
Nov 15 '10 at 22:52
source share

Thanks guys for your answers, but I was able to solve the problem with another solution.

I did a separate action for the presentation and for the partial presentation. Partial view updates are performed using the following jQuery method.

 $('.class').click(function () { var form = $("#eventForm").serialize(); $.post("/Controller/Action", { form }, function (result) { $('#container').html(result); } ); }); 

and send is done using the "normal" send button.

0
Nov 16 '10 at 8:28
source share



All Articles