views:

571

answers:

2

I have multiple pages containing the same partial view. The partial contains a form that posts to an action. After a post I want to return to the page I was on before the post. What's the best way to do this?

Example:

Partial View: form post action = note/create/

Pages

page1: products/index/

page2: customer/details/

page3: order/details/

These 3 pages contain the partial view, when posting the partial it redirects to note/create/. I need to return to the original page on success.

Thanks Simon

+1  A: 

Either have the post happen via AJAX -- thus not leaving the page, or pass the current controller/action/id (or the Url as a whole) as parameters to the action that handles the post. See below for an example of the later.

 <% using (Html.BeginForm(...)) { %>

      <input type='hidden'
             name='currentController'
             value='<%= ViewContext.RouteData["controller"] %>' />
      <input type='hidden'
             name='currentAction'
             value='<%= ViewContext.RouteData["action"] %>' />
      <input type='hidden'
             name='<%= ViewContext.RouteData["id"] %>' />

      ...rest of form...
 <% } %>

or

 <% using (Html.BeginForm( ...,
         new { ReturnUrl = Url.Action( ViewContext.RouteData["action"],
                                       ViewContext.RouteData ) }, ... )) { %>

  ....
 <% } %>
tvanfosson
A: 

You can store the current page address in a hidden field and send it with Post request.

In your partial view:

<script type="text/javascript">

var field = document.getElementById("currentPage");
field.value=document.location.href;

</script>

<form method="post" action="note/create/">
...

<input type="hidden" value="" id="currentPage" name="currentPage" />
</form>

Then retrieve the address of the hidden input and redirect the user to it.

Marwan Aouida