Asp.NET MVC3 And LinQ Based Sample Web Application

In this tutorial, my goal is to assist you through an very simple web application development that utilizes asp.net mvc framework(more specifically version 3.0), linq to sql, and razor template engine, so that you can have complete idea of developing a database driven application with these technologies. We will be using Asp.NET MVC 3 and .NET Framework 4 for this sample application.

To Do Of This Tutorial:

In this tutorial, our goal is to become familiar with the fantastic ready made solution of asp.net mvc for show,insert,edit and delete data. Lets assume, we want to build a poll application. so, we will have to prepare a set of questions and their answers. In this tutorial, we will only see how to show, add, insert and delete questions.

Database Structure:

See the simple dbml structure to get the idea of this table. Although you can put the dbml file anywhere you want, its best to put that in the models directory,

Creating The model:

In the model directory, create a new model named ‘QuestionModel’. This will contains all our model implementation code block. Create and interface named ‘IQuestionService’ , which will be base and define the methods to be implemented.

Now create a new class named ‘QuestionService’ which implement the interface ‘IQuestionService’. Besides, this class should contain the declaration of an object of ‘MyDataContext’ type(according to dbml file). Also, we can implement the first part, which list all existing questions that means the ‘GetAllQuestions’ method. So, the constructor and this method should look like as follows:

Create New Page And Controller:

First create a new mvc application and select razor as the template engine as i described on my previous basic mvc 3 tutorial
Lets create a new page and its controller, that we will be using through out the tutorial. Open ‘Views’=>’Shared’=>’_Layout.chtml’ and add a new menu link Like as follows:

Notice the structure @Html.ActionLink(“Questions”, “Index”, “Question”) . First parameter is the text of the anchor link, second is the method name that will be called for the associated controller and third one the name of the controller. Save it. And create a new controller named “QuestionController.cs” and check the option for automatically creating action methods for create/update/delete.

Now, Create an instance of our model class here in the constructor and implement the ‘index’ method to send the question lists to view.

Creating The View:

Lets create the view for this index method. Instead of creating it manually, we can create it easily just by give some commands(I have already explain how to do it in my previous basic mvc 3 tutorial). Don’t forget to select the ‘Scaffold Template’ as ‘List’ while creating the view.Now please build the application and run it. Navigate to the ‘Questions’ Menu. You should See something like as follows:

As We have no data in database yet, we get this empty list. However, ‘create’ option won’t work also as we haven’t implement the controller method for that yet.

Implement Create Method:

You will see, among the automatically generated methods, there are two methods named ‘Create’, one is with ‘ [HttpPost]’ and parameter another is without attribute and parameter. Without one will be called when we show the create template to user. when user fills up the form and submits, with the ‘[HtttpPost]’ one will be called. withe the corresponding ‘Question’ object as parameter.

Implement ‘Delete’ Functionality:

Delete option also include 2 methods. It have a basic scaffold template also, which simply ask for confirmation from the user whether he really want’s to delete or not. Here is the code sample for the controller part of delete action:

So, if we rebuild and run the application again. We should be able to delete now:

Adding Validation On Forms:

Till now, we haven’t added any kind of validation to our application, neither client nor server. Asp.NET MVC 3 comes with a very nice feature of binding a model class to be validated nicely in both client and server end. We don’t have to even write any java-script code for client validation, they will be automatically generated. To add validations to the entity classes in dbml files, open the dbml in design mode, right click and select ‘view code’ option. On the code file, add the following code snippet:

Complete Code Reference:

To make it more easier, I am going to share the both model and controller codes together here. As we didn’t had to touch the view part at all(all were generated automatically except the layout part that i have already given above), I am not going put their code here.

Hope this tutorial on asp.net mvc3 and linq with razor template easier is helpful to you and now easy for you to do as your home practice 😀 . Let me know if anything isn’t clear enough by commenting below. Happy coding 🙂

If i add one more column QuestionType to the given model and there is a seperate table that stores the QuestionType, how can i use a dropdown list, that contains all the QuestionTypes while creating new Question and while Editting existing Question?

Is there any samples or tutorials that discus this using LINQ_SQL model or can you help me to do the same