Solved

Do MVC2 Views have Postback?

Posted on 2010-09-23
9
942 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

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

[HttpPost]
0
 
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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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