Okay. My first few looks at ASP.NET MVC left me disillusioned and despondent. It seemed that Microsoft had missed the whole point of MVC. It is meant to abstract the presentations from the data to be presented, but Microsoft seemed to using it as a way of obfuscating the code and making it impossible to determine where the data you are looking at came from or how it was generated.
That view is slowly changing… slowly.
Here are a few things that have helped me in understanding what the heck in going on in ASP.NET MVC:
1) In ASP.NET MVC specific portions of code reside in specific folders with specific names.
Your controllers sit in a folder named Controllers and the source file is named xxxController.cs. Your Views are in a folder called Views, etc. You don’t get a choice – MVC is inflexible. If you don’t name your files properly and put them in the right places then it won’t work. It makes me wonder if Microsoft thinks of MVC as an illustration, not an acronym: the M and C stand for Movable Code and the V is the head of axe separating the two.
I’m not saying this is bad. Just that you need to know this before jumping feet first into ASP.NET MVC.
2) In ASP.NET MVC actions map to methods on the controllers.
This may seem super-simple and obvious to you if you work with ASP.NET MVC. But for those of us that haven’t touched it before, this is a super-critical piece of information. That one sentence alone replaces four to five bottles of Advil.
3) ASP.NET MVC is not MVC.
Before all you ASP.NET MVC programmers go rabid, let me explain.
MVC stands for Model-View-Controller. It is a design pattern that allows for one or more models (holding data) to be used interchangeably with one or more views (presenting data) through the use of a controller that can talk to both. In MVC, M stands for the model, V for the view and C for the controller.
ASP.NET MVC addresses only a portion of the MVC pattern. In particular, it allows for the creation and communication between the views (V) and the controllers (C). However, it does not provide any framework for creating models or communications between the models and the controllers.
So what is the M in ASP.NET MVC? What are all those objects under the Model folder? They are the view models: the temporary structures used to pass information between the controllers and the views. They are NOT the data models (M), which hold the application’s data.
Again, once you know that Microsoft thinks MVC stands for viewModel-View-Controller and not Model-View-Controller, then ASP.NET MVC code makes a lot more sense. But if you make the mistake that thinking the M means Model, then there is naught but hair-loss and ulcers in your future.