Tuesday 11 October 2011

HTML Helper: Html.BeginForm

@using (Html.BeginForm())

This generates a URL for the Form to send the request to and it will refer to the CURRENT action = that is, the one that processed the current request.

Useful where you want the same URL to handle GET and POST requests.

To do this you need two action methods with the same name, one decorated with the [HttpPost] attribute (an action method selector) and that takes an instance of the View Model:

public class SomeController : Controller {

    public ViewResult MyAction() {
        /* Displays the form */
    }
    [HttpPost]
    public ActionResult MyAction(MyModel incomingData) {
        /* Handles the POST */
    }
}

No comments:

Post a Comment