Now let us start with a step-by-step approach from the creation of simple MVC application as in the following:
- "Start" -> "All Programs" -> "Microsoft Visual Studio 2010".
- "File" -> "New" -> "Project..." then select "C#" -> "ASP.NET MVC 4 Application" then choose internet application.
- Provide the website a name as you wish and specify the location.
- Now after adding the MVC project, the Solution Explorer will look like the following:
- App_Start folder
The attributes you have seen on each property is included in the namespace System.ComponentModel.DataAnnotations that validates the input values at the client side as well as the server side at the same time time as using the jQuery library files. We will learn it in detail in my next article. I hope you have understood the concept of model.
To create the controller, right-click on the Controller folder and click on Add new controller. It shows the following window, provide a name for the controller with the suffix controller and then choose empty MVC controller from templates as in the following screenshot:
Now to add a View. Now that we have created the Controller, let us create the view for the AddNewEmployee action method. Right-click near to the ActionResult method definition and click "Add View...:".
- Provide the view name in the free text area as shown in the following window. By default the view is created with the name ActionResult method.
- Choose one of the view engines from the given dropdownlist that are Razor or Aspx ,we have selected the Razor view engine.
- Check the checkBox to create a strongly-typed view.
- Now choose the model class for a strongly-typed view from the given dropdownlist. In our example we have choosen EmpModel that we created.
- Choose the Scaffold template that will create an automated UI depending on the model class. In our example we have chosen to create a template.
- Check the References script libraries checkBox that will add jQuery libraries to validate the model input values during execution.
- Now check the user layout or master page layout checkBox that will add a shared page that provides a consistent look across the applications.
- Choose the view using the browse button of the following window. In our example we have selected _Layout.cshtml.
- Click on the Add button.
Now after clicking on the Add button it creates the following view with a rich UI design. The following default code gets generated in the view:
Now write the following code in the RouteConfing.cs file:
- EMP: our controller name
- AddNewEmployee: our action (method) name
- Id: optional parameter for the action (method)
Now the complete code of Emp controller will look as in the following: