As I pointed out in my blog post a few days ago, I see several reasons why you should make the switch to ASP.NET MVC. On the other hand, you have to remember that ASP.NET WebForms has been around for many years, and is a very mature technology. We all know that new technology has its initial flaws.
I was fooling a little around with the technology, specifically some databinding where I had a DataTable and wanted to display a Table with the contents. My instant thought was: “This is like databinding back in the days of Classic ASP”…
Take a look at this:
<html>
<head>
<title>Get data from database</title>
</head>
<body>
<%
' Data connection
Set Conn = Server.CreateObject("ADODB.Connection")
DSN = "DRIVER={Microsoft Access Driver (*.mdb)}; "
DSN = DSN & "DBQ=" & Server.MapPath("/cgi-bin/database.mdb")
Conn.Open DSN
' SQL statement
strSQL = "Select * from Employees"
Set rs = Conn.Execute(strSQL)
Do
Response.Write rs("Test") & "<br>"
rs.MoveNext
Loop While Not rs.EOF
Conn.Close
Set Conn = Nothing
%>
</body>
</html>
Good old Classic ASP that is. You’ve got to admit that you had seamless control of the HTML that was being rendered!
Which is also the case with ASP.NET MVC. MVC offers you control and simplicity, as opposed to WebForms where you place a server control on the form, and sometimes you ended up with your HTML a complete mess…
Of course, MVC is a lot more maintainable and structured, than Classic ASP was. You instantly get a separation of concerns with the Model, View and Controller design pattern.
So we can fetch data in our Controller:
public ActionResult Index()
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Rows.Add("Test 1");
dt.Rows.Add("Test 1");
dt.Rows.Add("Test 1");
dt.Rows.Add("Test 1");
dt.Rows.Add("Test 1");
dt.Rows.Add("Test 1");
ViewData.Model = dt;
return View();
}
And display it, somewhat like we did back in the days of Classic ASP:
<% foreach (System.Data.DataRow item in ((System.Data.DataTable)ViewData.Model).Rows)
{ %>
<%= Html.Encode(item[0].ToString()) %><br />
<% } %>
To be honest, I actually likes going back to basics and getting full control of my HTML. Working in WebForms, you sometimes search for control settings to enable or disable the rendering of a certain piece of HTML. I think it is a lot easier to just give you full control.
But on the other hand, I could easily see a lot of the Classic ASP ‘bad habits’ return. Classic ASP can be *ugly* as hell, which means the maintainability is very poor. The same could happen with MVC if you aren’t disciplined enough. WebForms sort of put you in a box, but you could still make it look pretty ugly as well…
ASP.NET MVC Release Candidate links
ScottGu’s release candidate blog post
Stephen Walther’s guide to learning ASP.NET MVC