Obviously I've been putting this off a little. I think the controller has been haunting me, it's very intimidating. I've become such a good friend to System.Web.UI.Page, it's hard to imagine my life without him. However, we all get older, grow apart. It's time for me (and you) to go head first into the next layer of MVC, we've started our project, set up our model. Now it's time for View -> Controller.
The thing about the controller is, it's a lot like normal code behind. The biggest adjustment? There is no page load. The View gets all its data from the Controller at runtime. There are a few key terms we need to learn. The first one is ActionResult. This is going to be a little confusing so strap in. The Action Result is what each method returns with relation to the View. Each method in the controller that returns data to a view is called an "Action Method"
To demo this we're going to do something super easy, we're going to do some simple math and post the result as a string on our page. Sounds easy? I think so too, don't forget, we're doing this for the first time together.
I must digress for a moment and move over to our global.asax. We're going to look at a method it's called RegisterRoutes and what it does is let our application know how to look at the Urls we're passing it. The way it's set up now, the first term being passed is the name of our controller, MVC now knows what to do with that, and the second term is the name of our Action Method, the third term will be any parameters, and so on. MVC has some serious RESTful goodness, so the RegisterRoutes method shows our application what to look at and where to go when it gets one of our pretty Urls. I left in the defaults, you can customise it for your application, take a look:
Also, before we get started, we need to cover something that's very important in MVC, and that's naming. In order to implement the intuitive functionality of MVC we need a few things to be the same. The name of our controller (plus the word "controller"), and the folder that contains our views related to it (as children of the "Views" folder). Mine is called CapsuleController.cs and My folder is called Capsule. Two more things that need to match are the name of your view, and the name of your action method that returns the view (though not always, it does make things more convenient). Capsule contains a view called Math and we have a an action method in our controller called Math. Take a look at our tree:
This is going to look weird the first time you're looking at it. Our action method is being passed no parameters, this is the action that automatically is called by the controller when our view (of the same name) is loaded. If you have named your view (or action) something different, you can just call View("ViewName") and pass the name of the view you want into your method. When this guy is called we get our page as is, with no data passed in.
I think maybe that last C# code snippet is incomplete?
ReplyDeleteSara,
ReplyDeleteI'd not access the link property using the Form object.
It's better to have the action read into the FormCollection object instead of the Request.Form collection.
Or even better, just pass the link as parameter for the action
Nice write up!
ReplyDeleteI do agree with @simone and not access the Link value via the Forms property of request. You can easily pass the information to the view through a ViewModel.
This way you have clear path between view and controller and know the specifics of data that gets passed along.
agreed and changed!
ReplyDeleteHi vey nice interesting blog im from leeds i found this on msn I'm in the crane hire company business, i found this blog very interesting good luck with it i will return to this blog soon
ReplyDeleteNice content, I think this is a nice blog. Keep going.
ReplyDeleteWhat a wonderful blessing indeed. I love good news like this.
ReplyDeleteHer advice to me was to work harder!!
ReplyDelete