Solved

Do MVC2 Views have Postback?

Posted on 2010-09-23
9
943 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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