Solved

Do MVC2 Views have Postback?

Posted on 2010-09-23
9
941 Views
Last Modified: 2012-05-10
My ASP.NET was never that strong, which is why I like the idea of my latest job to create a web app. using MVC2.  But I need to learn more before I am c onfident as I'd like to be with it.

Does the HTML get a postback?  I read that there is none.

Please explain this and also, why the Nerd Dinner MVC2 demo uses terms like GET and POST, and what they may mean otherwise.

Thanks,
newbieweb
0
Comment
Question by:newbieweb
  • 5
  • 4
9 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33748114
It does post back..... sort of......but not in the traditional sense. The initial request to a page is usually a GET (i.e. request for information) which, if there are no parameters, results in a Create() action in the controller. When you submit data this results in a POST request to the server, which, for a new record, will result in a second Create call, only this time to an overload that accepts an instance of your model.
0
 

Author Comment

by:newbieweb
ID: 33748248
That helps.

What does this do?:
Html.BeginForm()

and is it related to a POST?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33748310
Html.BeginForm() is a Html helper method. It basically marks the start of the Form element in the page. It also helps to hook up the View to the correct Action and Controller, there are several overloads available if you want your form to target a different Controller or Action than it's default.
0
 

Author Comment

by:newbieweb
ID: 33748377
And please explain what this attribute does:

[HttpPost]
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33748395
It marks an action on your controller as being responsible for handling POST requests.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33748418
So, for example, if you had a controller that handles creating new Customer you might have a controller like:
public CustomerController : Controller

{

    public ActionResult Create()

    {

        // handles the intial GET request that presents a View to allow data entry

    }



    [HttpPost]

    public ActionResult Create(Customer customerToCreate)

    {

        // handles the POST request when a user populates the View and submits it

    }

}

Open in new window

0
 

Author Comment

by:newbieweb
ID: 33748505
I am still not getting it.  In your example, how would how differently would the Create() function act if [HttpPost] were not used?

Also, I have this text from Nerd Dinner that highlights my lack of understanding.  Where is the GET and POST being received to differentiate between them?  

When the [HttpPost] attribute is applied to overloaded action methods, ASP.NET MVC automatically
handles dispatching requests to the appropriate action method depending on the incoming
HTTP verb. HTTP-POST requests to /Dinners/Edit/[id] URLs will go to the above Edit
method, while all other HTTP verb requests to /Dinners/Edit/[id] URLs will go to the fi rst Edit
method we implemented (which did not have an [HttpPost] attribute).
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33748570
I'll try and break this down into steps.

When you open a brand new browser window an point it at something like "http://NerDinner/Dinners/Edit/5" you are making a GET request to the Edit action of the Dinners controller for record id 5.
The view will then present you with a form to edit that dinner.
When you click Save (or whatever button is there) you are making a POST request (i.e. sending data back to the server) to the same URL.
In MVC terms this gets redirected to the Edit action of the Dinners controller and because it is a POST request the call gets directed to the version of Edit that has the HttpPost attribute applied to it.
0
 

Author Closing Comment

by:newbieweb
ID: 33748907
Thanks for all the help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eagerly loading related objects in entity framework 5 26
Vb. Net application freezes 9 30
Variable Event ? 3 23
Windows Media Player 2 11
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now