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/
部分视图:表单发布操作= 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.
这三个页面包含局部视图,当发布部分视图时,它会重定向到note / create /。我需要在成功时返回原始页面。
Thanks Simon
2 个解决方案
#1
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.
要么通过AJAX发布帖子 - 因此不要离开页面,要么将当前控制器/ action / id(或整个Url)作为参数传递给处理帖子的操作。请参阅下面的示例。
<% 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 ) }, ... )) { %>
....
<% } %>
#2
You can store the current page address in a hidden field and send it with Post request.
您可以将当前页面地址存储在隐藏字段中,并使用Post请求发送它。
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.
然后检索隐藏输入的地址并将用户重定向到它。
#1
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.
要么通过AJAX发布帖子 - 因此不要离开页面,要么将当前控制器/ action / id(或整个Url)作为参数传递给处理帖子的操作。请参阅下面的示例。
<% 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 ) }, ... )) { %>
....
<% } %>
#2
You can store the current page address in a hidden field and send it with Post request.
您可以将当前页面地址存储在隐藏字段中,并使用Post请求发送它。
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.
然后检索隐藏输入的地址并将用户重定向到它。