Web Forms vs. MVC

Having been with Venn for close to 6 months now, one thing has stood out to me – the ability to quickly develop websites with MVC. MVC is a design pattern – a way of coding – that forces the separation of three key components of web development – display, logic and input.

Diagram of an MVCMVC stands for Model-View-Controller. The model is the first level, often communicating between your database and your controller. The controller meanwhile is receiving this input and sending it to the view, and also sending inputted data back to the model. Of course, the view is the output that you or I might see, that would otherwise leave you none-the-wiser to the goings on in the background.

So, why not Web Forms?

First off, and perhaps most importantly, MVC can deal with multiple forms on one page, something that Web Forms isn’t so good at. This is great especially for Venn, as several of our clients’ sites have multiple forms on one single page.

There is also less development from the developer’s point of view. A typical form with some textboxes, a few checkboxes, a couple of dropdowns and a submit button, would likely have to be built manually, inserting one control for every item in the form. Then on the other side, getting the text from each textbox, checking it for a value and then continuing with whatever process is needed. With MVC however, a model can be written with a collection of variables. The form can be generated with this using standard HTML mark-up, and the data can be immediately passed back as the same type of model.

Validation is also a major advancement in MVC. How often have you used a form on a webpage, and found either an error or that the form never even submitted in the first place because of lack of validation? It can be frustrating pressing the submit button to then find you missed a field or you wrote your e-mail address badly, but even more so when you try and go back and find some of that information you filled out has gone. MVC introduces validation messages for every item in every model, if you wish to include them. Linking the request for validation to a particular model variable will stop the form even being submitted if something isn’t right. It’s even possible to write multiple validations for each field and define the look and feel of your error messages.

Finally, Sessions. Sessions were a big part of Web Forms, being used to keep a user logged in, or to hold their name so that each page on the site could have a customised error message. It’s true that Sessions may still be used for the login system, but these heavy never-dying beasts are generally much less in number. Instead, MVC introduces different forms of temporary data storage between pages, allowing the developer to keep data for just as long as it is needed and no more.

In defence of Web Forms

Web Forms has been around longer, and no matter how old MVC and Web Forms get, Web Forms will always be more established. Web Forms are also great for new developers – they happened to be the way I came into .NET development. They are almost plug-and-play, with you needing to drop a few textboxes and a button onto a webpage design to start with. The complexity increases as you develop, depending on what you choose to incorporate.

Once you learn Web Forms, you can do a Windows form application. These are more like your desktop programs, and are created in exactly the same way with the same controls and the same programming style.

If I had to weigh up both design patterns, I’d say that Web Forms may be easier for a first time developer as it exists with similarities that you see on every day forms – textboxes, dropdowns and buttons. That said, MVC is easier for websites with multiple forms and for websites that demand strict boundaries between program logic and algorithms, as well as the output.

I don’t think for a minute that MVC will completely replace Web Forms, due to them being very well bedded in and suited for first-timers. I can, however, see MVC giving Web Forms a very good run for its money. You know how things progress though. Ask me again in 5 years’ time!

Image adapted from w3schools