开发者

submit to same action in MVC

In my View (.aspx) code,开发者_如何学C what parameters can I provide to Html.BeginForm() in order to get it to submit back to the same controller and action that produced the view?


It all depends on your route table. Assuming that you're using standard routes, I think you can provide no arguments (or nulls) and that you'll end up at the same controller-action that generated the view.

Otherwise, you can pull the current controller and action from the route data.


Using the ViewContext you can get the route data that was called

<% using (Html.BeginForm(ViewContext.RouteData.Values["controller"].ToString(), ViewContext.RouteData.Values["action"].ToString(), FormMethod.Post)) {%>
    your form data here
<% } %>


In your view

<% using (Html.BeginForm("actionName", "controllerName", new {  }, FormMethod.Post, "")){ %>
<%} %>

if you are not using html helpers

<form id='form' action="../controllerName/actionName" method="post">

In your controller

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult actionName (FormCollection collection)
{}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜