Table of contents
1.
Introduction
2.
MVC Architecture in ASP.NET
2.1.
Model
2.2.
View
2.3.
Controller
3.
Integration of Model, View & Controller
3.1.
Create Controller
3.2.
Create Model
3.3.
Create View
3.4.
Integrate Model, View, Controller
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET MVC Integrate Model, View & Controller

Author Toohina Barua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ASP.NET MVC is a web application framework for the.NET Platform that uses the Model-View-Controller paradigm to create full-stack web applications. Model, View, and Controller are three components that must be integrated and combined to form a complete web application. From this article, we will learn how to integrate these components using an example. So let’s dive in.  

MVC Architecture in ASP.NET

MVC is a software architectural pattern that adheres to the separation of concerns principle. Model, View, and Controller are the three pieces of a web application that are interrelated.
The MVC pattern aims to allow each of these components to be built, tested, and merged to produce a powerful application.
Let's take a closer look at each one:

Model

Model objects are components of the program that implement the data domain's logic. It uses a database to obtain and save the model's state. A product object, for example, may receive information from a database and perform operations on it. Then, in the SQL server, write the data back to the products table.

View

Views are the components used to present the application's user interface (UI). It demonstrates the. Model data is used to develop ASP.NET MVC applications.
An edit view of an Item table is a frequent example. Based on the current status of items and objects, it displays text boxes, pop-ups, and checkboxes.

Controller

Controllers manage user interaction, deal with the model, and choose which view to render for Ul. The view just shows information in a.Net MVC program; the controller controls and responds to user input and interaction using MVC action filters. The controller, for example, controls query-string data and transmits them to the model.

Source

Integration of Model, View & Controller

From the above section, we learned about MVC architecture in ASP.NET. But Model, View, and controller are separate components. They need to be integrated and put together to get a full-fledged web application. We will learn how to integrate them using a practical example code. Let’s make a student database management web application. Below is the step-by-step guide on how to make this application.

Create Controller

As seen below, in the StudentController.cs file inside the Controllers folder, create the StudentController class using the Index() function. This may be done directly in the Visual Studio GUI.

StudentController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_BasicTutorials.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            return View();
        }
    }
}


As you can see above, the StudentController class is inherited from the Controller class. Every MVC controller must derive this abstract Controller class. This fundamental Controller class has several auxiliary methods that may be used for various things.

Create Model

In the model folder, create a new Student class. This sample class will hold the students' id, name, and age. So, as illustrated below, we'll need to add public properties for Id, Name, and Age.

Student Model class

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set;  }
    public int Age { get; set;  }
}


The model class may be used to fill data in the display and to communicate data to the controller. 

Create View

Create an Index.cshtml page in Student folder inside Views folder. This contains the code for what the user interface would look like.  

Views\Student\Index.cshtml
@model IEnumerable<MVC_BasicTutorials.Models.Student>

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
   
    <h2>Index</h2>
   
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.StudentName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Age)
            </th>
            <th></th>
        </tr>
   
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.StudentName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
                @Html.ActionLink("Details", "Details", new { id=item.StudentId  }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
            </td>
        </tr>
    }
   
    </table>


It contains both HTML and razor codes, as shown in the Index view above. The @ sign is used to start an inline razor expression. @Html is a class that helps you create HTML controls.
We need to pass a model object to a view to display the data on the view.

Integrate Model, View, Controller

We now need to send a model object from an action method to a view to run it correctly. As you can see in the preceding Index.cshtml, the model type is IEnumerable<Student>. As a result, we must supply it from the StudentController class's Index() action function, as seen below.

Must Read IEnumerable vs IQueryable.

Passing Model from Controller

public class StudentController : Controller
{
    static IList<Student> studentList = new List<Student>{
                new Student() { StudentId = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentId = 2, StudentName = "Steve",  Age = 21 } ,
                new Student() { StudentId = 3, StudentName = "Bill",  Age = 25 } ,
                new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } ,
                new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } ,
                new Student() { StudentId = 4, StudentName = "Chris" , Age = 17 } ,
                new Student() { StudentId = 4, StudentName = "Rob" , Age = 19 }
            };
    // GET: Student
    public ActionResult Index()
    {
        //fetch students from the DB using Entity Framework here

        return View(studentList);
    }
}


We've constructed a list of student objects for demonstration purposes, as you can see in the code above (in real-life applications, you can fetch it from the database). We then provide this list object to the View() function as a parameter. The base Controller class defines the View() function, which automatically links a model object to a view.
You can now start the MVC project by clicking F5 and going to http://localhost/Student. In the browser, you'll see something like this.

Source

Check out Entity Framework Interview Questions to know more about it.

FAQs

  1. Which would be the proper framework to be used: ASP.NET MVC or ASP.NET Web API?
    ASP.Net MVC is used to create web applications that return both the view and the data, whereas Asp.Net Web API creates all HTTP services in a simple and basic approach that only delivers data, not the view.
     
  2. From where is the view derived?
    Every view in the ASP.NET MVC is derived from WebViewPage class included in System.Web.Mvc namespace.
     
  3. What is Razor? 
    In ASP.NET MVC, one of the view engines supported is Razor. Razor lets you to use C# or Visual Basic to construct a combination of HTML and server-side code. The.vbhtml file extension is used by Razor views written in Visual Basic, whereas the.cshtml file extension is used by Razor views written in C#.
     
  4. ASP.NET is open-source. Explain.
    Microsoft has made the entire.NET server stack open source, making it a 'free' download. This comprises ASP.NET, the.NET compiler, the.NET Core Runtime, Framework, and Libraries, allowing developers to create applications with.NET on Windows, Mac, and Linux.
     
  5. What is ASP.NET Core?
    The open-source and cross-platform version of ASP.NET is called ASP.NET Core. Prior to the release of ASP.NET Core, the Windows-only versions of ASP.NET were simply referred to as ASP.NET.

Key Takeaways

We learned the MVC Integrate model, View, and Controller in ASP.NET using a practical coding example from this article. 
But this is not enough; you need something extra to excel in web development truly. If you want to learn more about web development, you can read our articles or take our highly curated Web Development course.

Recommended Readings:

Live masterclass