Ok, so, do you remember learning about HTTP Get and HTTP Post in school? Ok, do you remember when you started building Web Forms and you just chalked that up to another thing that you learned in school that's not applicable in the real world? Well, guess what. MVC is 100% get and post. Never fear, it's an easy concept. Once you get it you will realize it's truly awesome to be working with the web at its root.
Now, for those of you who's memory is a bit fuzzy http stands for Hyper Text Transfer Protocol. It's the way things are sent from your computer to the server and then back. It's an integral part of what we do, and I appreciate that MVC brings us closer to the action.
Today we shall create one page that does both things, a Get AND a Post. We're going to create a new project for a restaurant named TGIW (this place seriously loves Wednesdays, weird). We've already learned about Views so I went ahead and built that myself. Today we're going to learn about two big things: Rest and populating our views with data.
REST stands for Representational State Transfer and it was actually created by one of the inventors of HTTP. In REST calls individual requests are made using URIs like hypertext links. You can make GET or POST requests by using URLs. This is how MVC performs its actions.
Now, what we want to do is have our daily specials loaded as the form comes up, and then send back our order. Our application is using LINQ to SQL and we have two tables, “Specials” that contain our daily specials, and “Orders.” which is where our orders go.
What we're going to do first is create a ViewModel. MVC is very good at simple CRUD behavior with data objects, however, when our application requires data that is more complex the best way to pass this data into our View is through a ViewModel. Since our view needs to be able to manipulate multiple classes we're going to aggregate them in a ViewModel.
Our ViewModel is called OrdersViewModel, and it includes a constructor that gets our special of the day via by creating a special and passing the day of the week.
Now we come to the “Get,” we're going to build an action that returns an ActionResult that populates our page. Inside our action we're calling a method called that accepts a string “day” and returns our special. When we return our View, we're going to pass in that special. We reference the special on our page.
In our view we're going to make sure that the type passed into “System.Web.Mvc.ViewPage” is our “OrderOnlineViewModel” class, because that's what we're binding our page to. This allows us to reference any of the values in that class. We will display today's special, I'm going to throw it in a div. We use Model.PropertyRefrenced as the syntax.
So, next we're going to do our Post. We have our text boxes, where we can input how many meals we want to order (this place has a very limited selection). We're going to create our Post action the same way we did our Get, except this time we will be passing IN a our Order to be saved. Within our post we put all the data we grab all the data collected in our form. There are a few ways to do this. The first step, however, is to make sure all our html controls are named in a way that we can reference in our controller.
This brings us to an excellent point that we need to go over, the concept of “Convention Over Configuration.” I don't know if you've noticed but this has been floating around a lot recently. It basically means that it's better to agree on a way that things should be set up and make that the way you do things, than it is to manually configure each time. This concept is exhibited in ASP.NET MVC in the way that controllers have to be named after the folder where the views are contained, and how the values on the view are referenced why whatever the controls are named.
For this project we're going to use simple text boxes right now, we'll move onto more involved controls. All we need to do is create a textbox to enter our data, then on our controller we're going to name our action the same thing as our “get,” this time however we're going to specify that it is a post. We did this in our last edition, and now we're doing it the same way. Don't forget to specify that your form has a default action of “Post” and you'll be set, your most method will automatically submit after your click event.
Next up: the HTML Helper, and ways you can use it.
I apologize for the code images, will embedd code in the future. For now you can download the source here.
I enjoyed the first 3 articles - but this last one was a doozy to follow. I'd like to see the source code when you have a chance to put it up here. The current mention at the end of the article doesn't link anywhere.
ReplyDeleteDenny - sorry, I was having trouble with my file server. Fixed!
ReplyDeleteWas it the content or the way it was presented?
Great post. Thanks for sharing the code.
ReplyDeleteThat development scheme should make it easier to make reliable code that can be reliably enhanced over time. If this is a paradigm that even a decent percentage of ASP.NET developers utilize, there should be an increase in application quality coming down the road.
ReplyDeleteHai Sara, Thanks for the article. Can you upload a sample with VS2008 version?
ReplyDeleteJust following this series of posts, thanks for these, they are very straight forward.
ReplyDeleteI assume this example is just illustrating the basic concept of ViewModels, but is it the case that we just use our existing serializable Business Objects (or DTO objects) as the ViewModel objects?
ok, just read the next post in the series and think I get it now...I suppose one could use existing serializable business objects or DTO's if the content of them are all that's required. Its seems ViewModels are basically combinations of these to allow for single transfer of all the data for GET? Is my thinking here correct?
ReplyDeleteI wouldn't have thought about doing it this way at all, especially with integrating them with a 3rd party. But you're right, it does pay off in the real world doesn't it?
ReplyDeleteI like your website, It has been a pleasure to read the different articles in it, I'm subscribing to your rrs feed right now!
ReplyDeletea href=http://www.sharmawebsolution.webs.comsharma web solution/a
a href=http://www.sharmawebsolution.blogspot.comsharma web solution/a
Above these goods really good, so beautiful jewelry!Really to becommended!
ReplyDeleteWith your help, i learned more about asp.net MVC. Thanks.
ReplyDeleteIf predictable is the default for page, then I guess that you guys still must update this page
ReplyDeletemsdn.microsoft.com/.../system.web.ui.control.clientidmode(VS.100).aspx
Perhaps when .NET 4.0 will be released this will be updated?
The default value of ClientIDMode for a page is AutoID. The default value of ClientIDMode for a control is Inherit. If you do not set ClientIDMode for a page or for any controls on the page, all controls will use the AutoID algorithm.
I am currently studying for the beta exams and I was wondering what should I answer in case there's questions regarding this subject in the exam.
Perhaps I am thick. Is the mechanism you described for choosing a default value the approach to take when editing and matching the the selection to the users previous choice?
ReplyDeleteI don�t know If I said it already but this so good stuff keep up the good work. I read a lot of blogs on a daily basis and for the most part just wanted to make a quick comment to say I�m glad I found your blog. Thanks.
ReplyDeletegreat work dear
ReplyDeletenice job.
Remarkably all liked and handy good article.
ReplyDeleteHi, Looking for a reliable and lowering cost ASP.NET MVC Hosting, I've arrived to the page
ReplyDeletewww.webhost4lifereview.com/.../aspnet-mvc-host
to check some information about MVC Web Hosting. Seems Webhost4life.org is one could be my choice, any other recommendations?
Thanks As you saw in the videos that's the focus I've taken and each video and shows how to accomplish basic tasks that are probably familiar to you you from web forms and As the series progresses I'll continue to demonstrate WebForms Concepts translated to MVC................ www.classifiedneeds.com/.../city.php
ReplyDeleteThat is a truly fantastic piece of work.
ReplyDeletejust wanted to let you know I enjoy reading your blog and hope you will continue writing them for a long time to come.
ReplyDeleteA blog is not a one-way street.
ReplyDeleteAll we need to do is create a textbox to enter our data, then on our controller we're going to name our action the same thing as our “get,” this time however we're going to specify that it is a post. We did this in our last edition, and now we're doing it the same way. Don't forget to specify that your form has a default action of “Post” and you'll be set, your most method will automatically submit after your click event. http://www.mbtusasale.com
ReplyDeleteOk, so, do you remember learning about HTTP Get and HTTP Post in school? Ok, do you remember when you started building Web Forms and you just chalked that up to another thing that you learned in school that's not applicable in the real world? Well, guess what. MVC is 100% get and post. Never fear, it's an easy concept. Once you get it you will realize it's truly awesome to be working with the web at its root. http://www.uggsaleaustralia.com
ReplyDelete