MVC: Action Methods using GET & POST

Enabling specific actions for GET and POST calls.

MVC provided an elegant method to specify actions based on the type of call used by the webpage – GET (default) Http verb or POST Http verb.
To illustrate this ability, I’ll begin where my Form Validation article left off with a Controller, Model, and View (displaying a user-input form) already created.

As you’ll see in my Model, I’ve updated my first action method with the “[HttpGet]” attribute to denote it will respond with the webpage uses the GET call.
Also, I’ve added a new action method that will specifically respond when a page posts data back.

1-action-methods-get-post

Now I will create another strongly-typed View to display a confirmation message so the user knows that we’ve received their input.
Notice in my [HttPost] action method that I’m passing the name of the View – “Thanks.” Since the View is strongly-typed, I have immediate access to the Model’s fname and lname properties.

2-action-methods-get-post

Upon submitting the form with my information, I now receive a confirmation message including the name I entered within the “Thanks” View.

3-action-methods-get-post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s