I was thinking about MVC since I read first post on ScottGu’s blog. Yesterday, on Friday night, I decided to create my first MVC applications. I downloaded Visual Web Developer 2008 Express Edition 2008 last night. That 128 MB download took quite a long time to download. Then I downloaded MVC preview 5. After successful installation of MVC, I was ready to fly…
Finding a suitable example on the web:
Before starting develop a new MVC project, I needed a good example. After searching the web for a while, I found two links that seems to be useful.
First was a 4 part tutorial written by ScottGu. I was a detailed introduction to MVC framework and how it works. I was like a step by step DIY guide. Scott demonstrated how to create a complete e-commerce site using MVC framework.
The second choice was a simple To Do List application. This application allowed one to enter a list of tasks, save these into database and mark them complete. Both applications used LINQ to SQL for accessing database.
I decided to go with the Task List application as this was simple and small application. This app is like the typical Hello World example with database support. I took only one hour to complete this app and run it properly.
Starting the new project
I opened VWD (Visual Web Developer0 and choose to create new project. MVC applications are developed as web applications. They are different from web sites. In the New Project dialog, I chose ASP.NET MVC Web Application. This automatically created a sample application in the solution explorer with three special meaning folders – Controller, Model and View. I deleted the classes and pages auto-generated by the WVD. The I proceed step by step as instructed in the example application.
Step By Step Progress Through Solving Issues:
Once I completed typing (yes, I preferred to type instead of copy-pasting it) the code and creating all the classes and pages, I was time to see the code in action. But as you know, no code runs for the very first time. Although VWD (and VS as well) minimizes the chances of syntax errors to zero, many other types of error always occurs in the code. I face following problems for which I again need to do some Googling.
The ViewData Problem:
“The name ‘ViewData’ does not exist in the current context”
Even before compiling the code, my Index.aspx view warned me that ViewData is undefined by placing a red line below this. After some unsuccessful silly adjustment to the code, I go for Googling. I found a post in ASP.net forums asking for the same question. ScottGu answered the post by saying – “Have you tried compiling the project? It could be that you haven’t done a compile since you added the page, and so intellisense with C# hasn’t picked up that you are deriving from a base-class yet”. This was very strange for me but I decided to go with Scott and compiled the code successfully. But when I run the code, the same problem reappeared.
After 20 minutes of unsuccessful Googling, I decided to use trial-and-error method. And finally I got the solution.
The root cause of the problem was that I added a Web Form in my Views folder instead of MVC View Content Page. The normal web form is inherited from System.Web.UI.Page class while an MVC View Content Page is inherited from System.Web.Mvc.ViewPage class. I change the parent class to ViewPage and it worked fine.
Primary key error in inserting the record:
Now I rum my application without any error. The Index page that displays a list of all tasks appears on the browser. I opened create page to add a new task. On pressing submit button, it gave primary key related error. The error was cause by the InserOnSubmit method of the LINQ generated class. The error was quite obvious that I forgot to define primary key on the table. I had an Id column with its Identity attribute set to on. So, ideally any insert statement should not have issue with the primary key. But to fix the error, I modified the table structure and declare Id column as primary key. But the problem still persisted. Like the above issue, Googling was unsuccessful in this case as well and I had to go for trial-and-error method.
The solution was quite simple. I had to open the dbml file which is used for LINQ-to-SQL. I deleted the table from this file and again dragged that table into dbml designer from Server Explorer. Pressed F5 and the page was opened successfully in the browser. Opened the create page, added a task and pressed submit button. No error…
I checked into database table and the record was successfully inserted.
So this was the experience of developing first MVC application. The overall experience was good and exciting. I liked the simple and easy-to-read URLs that were displayed in the browser’s address bar.
Keep watching for more in ASP.Net MVC