Table of contents
1.
Introduction
1.1.
What is a Model?
2.
Create a Model class
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET MODEL

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

Introduction

In this amazing blog, you will get an introduction about the model class in the  ASP.NET MVC framework.

A model is defined as a collection of classes that contains the business logic of the application(models are business domain-specific containers). Model is also used for accessing data from the database. The model class does not handle direct input from the browser.

What is a Model?

Models are also referred to as objects used to implement conceptual logic for the application. The model represents all the data in your application. It can be a table that you are storing inside SQL Server, or it can be a model, which will be a combination of multiple tables, and so on. A controller interacts with the model, accesses the data, performs the logic, and passes that data to the view.

In the ASP.NET MVC(Model-View-Controller) application, all the Model classes must be created in the Model folder.

Create a Model class

Let's understand model with an example:

If i want to retrieve employee information from this table TBL employee and then display that specific employee information in our MVC application, as you can see on the slide. 

To encapsulate employee information, I need to have an employee model class. So, let's go ahead and add an employee model class to our MVC project.

Add the Employee model class to the Models folder to encapsulate Employee information.

To do this:

1. Right-click on "Models" folder > Add > Class

 

2. Name the class as Employee.cs

3. Click "Add".
 

 

Employee.cs code:

using System;
using System.collections.Generic;
using System.Linq;
using System.Web;
 
namespace MVCDemo.Models {
	public class Employee {
   		public int EmployeeId { get; set; }
   		public string Name { get; set; }
   		public string Gender { get; set; }
   		public string City { get; set; }
	}
}
You can also try this code with Online Java Compiler
Run Code

 

Now let's Add EmployeeController class to the "Controllers" folder. 

To do this:

1. Right-click on "Controllers" folder > Add > Controller

2. Use EmployeeController as the name

3. Click "Add."

 

 

EmployeeController.cs code:


using System;
using System.collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;
 
namespace MVCDemo.Models {
	public ActionResult Details() {
    	Employee employee = new Employee() {
        	EmployeeId = 101,
        	Name = "John",
        	Gender = "Male",
        	City = "London"
    	};           
    	return View(); 
	}
}
You can also try this code with Online Java Compiler
Run Code

 

So, now we need to pass the employee model object that we constructed in EmployeeController to a view, so the view can generate the HTML and send it to the requested client. To do this step, we first need to add a view. 

To add a view:

1. Right-click on Details() action method and select "Add View" from the context menu.

2. Set

   a)View Name = Details

   b)View Engine = Razor

   c)Select "Create strongly-typed view" check box

   d)From the "Model class" drop-down list, select "Employee (MVCDemo.Models)".

3. Finally, click "Add."

 

 

Details.cshtml code:

@model MVCDemo.Models.Employee
@{
  ViewBag.Title = "Employee Details";
}
<h2>Employee Details</h2>
<table style="font-family:Arial">
   <tr>
     <td>Employee ID:</td>
     <td>@Model.EmployeeId</td>
   </tr>
   
   <tr>
     <td>Name:</td>
     <td>@Model.Name</td>
   </tr>
   
   <tr>
     <td>Gender:</td>
     <td>@Model.Gender</td>
   </tr>
   
   <tr>
     <td>City:</td>
     <td>@Model.City</td>
   </tr>
</table>

 

Output:


 

Frequently Asked Questions

  1. How should a model be structured in Model-View-Controller?
    While dealing with web pages that use MVC design patterns, the best way is to have one:one relation between views and controllers. Each view represents a whole webpage in your website, and it has a dedicated controller that handles all the incoming requests for that particular view.
     
  2. What is the main disadvantages of the MVC model?
    The main disadvantage of MVC Architecture is that it can't be suitable for small applications, which has adverse effects on the application's performance and design.
     
  3. What are the Major Razor Syntax Rules?
    The major razor rules are-
    - Razor code blocks are enclosed in @{ ... }
    - Inline expressions (variables and functions) start with @
    - Variables are declared with the var keyword
    - C# code is case sensitive
    - Code statements end with a semicolon
    - C# files have the extension .cshtml
    - Strings are enclosed with quotation marks
     
  4. Why ASP.NET is better than MVC?
    ASP.NET requires less expertise than MVC and is much faster to develop user-friendly web applications overall. (Knowledge of HTML is not needed) ASP.NET uses a more mature code-base and has a larger web control toolbox. It's more sensible to use ASP.NET if you rely on other parties for UI controls.

Key Takeaways

In this blog, we have discussed ASP.NET Model with a detailed example. We have also seen how to create a model class.

Recommended Reading: 


If you are pursuing a new career in Web Development, we suggest you get your fundamentals crystal clear with our Full Stack Development course. 

Happy Learning, Ninja!

This course will help you!

Live masterclass