Dot Net For All

Create an ASP.NET MVC application to display Data Collection

In this series of the article I will discuss about the ASP.NET MVC, the concepts and terminologies which we should be aware while working on this wonderful technology. In the first part of the series I will create an ASP.NET MVC application from scratch to display a collection of data in the tabular form. I will add a controller to the application and a corresponding view to the same controller which will be helpful to show the data.While going through the article I will discuss about the important concepts along with code examples. I will use VS 2015 to create the application.

Lets start by creating a new project , go to the templates and go to Web template and select ASP.NET web Application and name the project as StudentInfo.

As we are done with this step, we will get a new window where we can choose the ASP.NET 4.6 Templates. Select MVC from that window as shown in the figure below.

As we are done with all these steps we can see that the project is created with the default structure.

and if we run this project by pressing F5 we should be able to see a nice web application which is the default demo application for the ASP.NET MVC project. For this series of the article I will keep the layout and styling the  same as the reference application and will do the changes to it as we proceed.

After we have created a new project we should be able to see the default structure of the project as shown below.

Now whenever run the project and click  on some link on  the page of the website for some action, the URL changes and we are transferred to a new page. If you want to know about the basics about the web, request and response you can read this articleWhat routing engine does is that it searches for a controller and corresponding method in the controller for the action performed.

I will discuss about the concepts of routing in a separate article.Now for the time being we will add a new controller by right clicking on the controller project and naming it as Student controller as shown in the next three figures.

Fig 1

Fig 2

Fig 3

Now after adding the controller I will add a new class in the Models Folder named as Student.cs. Below is the code for the class.

    public class Student
    {
        public int StudentID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Marks { get; set; }
    }

and I have to change the code for the Index method as shown below.

  private static IList<Student> studentList = new List<Student>() {
 new Student() { FirstName = "Vikram", LastName="Chaudhary", StudentID = 1, TotalMarks = 80 }, 
 new Student() { FirstName = "Prasad", LastName="M", StudentID = 2, TotalMarks=90 },
 new Student() { FirstName = "Steven", LastName="Hawkins", StudentID = 3 , TotalMarks = 95}
 };
        // GET: ViewStudents
        public ActionResult Index()
        {
            var studentsModel = studentList;
            return View(studentsModel);
        }

Since I am not using any external data source for the data I have creates a static list in the class to display the data. Now I have to create the view for the controller’s index method.

Right click on the index method and you will get an option to add the view and proceed as shown in the below two figures.

Fig 4

Fig 5

And we should be able to see the view under the Views > Student folder. I have selected the List template while creating the view to bind the list of the student to the views. The layout page option is used to set the layout page which can be used to provide a common user interface for the application which was done by the Master page in ASP.NET and while all the necessary plumbing is done by the MVC engine to bind the Model class to the view but we should be aware of what is happening. In the top part of the view page we should be able to see the below code

@model IEnumerable<StudentInfo.Models.Student>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

Here in the above code you can see that the MVC has created the @model directive by referring the collection of student class.

There is one more property you can see in the code named ViewBag. This property is one of the way to transfer information from the controller to the View. It is dynamically typed object in C# that means you can add any property to it and it will be available inside the view to pull out and retrieve.

The Layout is the same which we set while creating the View. Suppose you do not want to add any layout you have to Set this Layout – null and you will not get any layout for your page.

The amp(@) sign is a way to tell the razor view engine here is a C# expression.

Now If I save and run the code by adding the Student/Index to the end of the URL I should be able to see my page with the details of all the student as shown in the below figure.

Now if I want to add a menu item Named “Student Info” in Place of Home, About and contact I should go the the _Layout.cshtml which is present in the Views > Shared folder and comment out the other three links and add one more action link as shown below code.

 <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    @*<li>@Html.ActionLink("Home", "Index", "Home")</li> //Commented
                    <li>@Html.ActionLink("About", "About", "Home")</li> //Commented
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>*@ //Commented
                    <li>@Html.ActionLink("Student Info", "Index", "Student")</li>
                </ul>
                @Html.Partial("_LoginPartial")
   </div>

and if we run the application we should be able to see the “student info” Menu item and if we click on that we will be able to see the view.

In this article I have displayed you how we can create and simple ASP.NET application to display the collection data on the browser which we used to do using DataGrid in ASP.NET. In my next article I will show how to edit one of these records and what are the type of plumbing we have to do in the controller.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview