Solved

Do MVC2 Views have Postback?

Posted on 2010-09-23
9
947 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

622 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