ASP.NET MVC: A General Overview (Part 3)
Published on October 8, 2010 | Filed Under MVCWelcome back! This is part 3 of our introduction to MVC. Yesterday, we went over the use of the controllers. Today, I wanted to finish up with an explanation of what Views are and also give my final thoughts on MVC, as a whole.
Notes About Views
MVC enjoys using Master Pages in Views. This isn’t a requirement and you honestly don’t even need to use them, but Master Pages really aren’t anything to shy away from. In my experience, I’ve always viewed Master Pages as being a way to ensure a constant flow of your site. There are ways to alter Master Pages to appear slightly different for each hosted page, but still have the same theme applied to them.
It should be noted that Views also hold your partial page views.
What is a Partial Page View?
A partial page view quite similar to user controls in ASP.NET web forms. You call a pre-defined block of ASP.NET code, which will render uniformly on any page that it’s dropped into. I like to think of these as “widgets”.
There are, however, some limitations to the partial page views in MVC. In ASP.NET web forms, you were able to create a custom object, which had properties that can be defined within the object declaration. This is no longer the case. The entire code for calling a partial page view looks like this:
<%Html.RenderPartial("mycontrol");
This will call the file mycontrol.ascx from your Views/Shared folder. You won't have the ability to send any sort of parameters to this, so you really need to be smart about implementation.
What are the benefits of MVC?
I realize that the original intent of this was to explain why you should consider switching. Below are a few of the points, which were my whole reason for switching:
- Web Forms are virtually dead
- The benefit here is that Web Forms cause a lot of server-side errors, because everything is stored in a ViewState. MVC does not use ViewStates, so your users are far less likely to stumble across a page that randomly errors-out, because their ViewState information did not match what was on the previous page.
- Sites are more reliable
- This is sort of grouped with my last point. Since MVC doesn't rely on ViewStates, you have less processing when a user requests a page. I probably sound like I'm a broken record here, but I really did hate ViewStates.
- URLs have become "pretty"
- Maybe "pretty" doesn't have as much to do with the benefit, but it's the fact that now your URLs appear cleaner in search engines. It may no longer be a huge benefit to drop the file extensions on your web sites, but from a user's standpoint, it's a little more appealing.
- Developers are forced to learn better programming practices
- I'm sure a lot of people out there will disagree with me, until they see it for themself. The intent of Web Forms was only to process form information on the backend code, but it has turned into a "free-for-all" repository for dumping code specific for the page. Now that MVC is around, developers are going back to their old habits of creating robust classes, which have wide-spread uses.
With Benefits Come Disadvantages
I won't claim that MVC is perfect. In fact, there is a huge learning curve. Here are a few of my gripes with MVC:
- You can't create Subfolders
- It's a bit unfair to say that you can't do it, because I have accomplished it with my own work-around and others have done so, as well. But MVC 2 uses these things called "Areas" (note that this is not available in the original MVC,) which create a subfolder for you to work with. The problem is that your subfolder can't easily contain another subfolder. You can't just drop it in there and make it work, without programming. I'm going to write a tutorial later on about how to accomplish subfolders.
- Gathering form information is no easy task
- I'll admit that, at this current point in time, I have not figured out how to properly collect form data. I have a way of making it work, which is very ugly and anti-MVC. The MVC Website has some information on how they get it to work, but I'm shying away until I have a real use for it, personally.
- Web Forms don't implement easily
- From what I've seen, you can't drop a web form in your Views. My work-around has been to create a folder elsewhere, set it as an application in IIS and make it use ASP.NET 3.5. I know there's a better way than this, so I'll have to revisit it.
- In-page user controls are gone
- Some people didn't even use this feature and actually found implementation to be quite annoying, but I'm thinking more about the companies who have created these drop-in controls and will need to find a new way to get their product to work.
That about sums up this series! All-in-all, I'm very impressed with MVC 2 and am planning on converting quite a few of my own websites to use this framework. I know a few nay-sayers, but most of the people I know seem to love it as much as I do.
If you have any questions or comments about this series, please leave a comment on one of the pages! Your views are always welcome.
~Derek Torrence