C# / C# Web Development with ASP.NET

Working with Models and Views

In this tutorial, you'll learn how to work with models and views in ASP.NET MVC. This includes understanding how to create models, how to use views to display data, and how to upd…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Introduces web development using ASP.NET for creating dynamic web applications.

Tutorial: Working with Models and Views in ASP.NET MVC

1. Introduction

In this tutorial, we will delve into the world of ASP.NET MVC, focusing primarily on the roles of Models and Views. We will learn how to create and manipulate Models, how to use Views to display data, and how to update data using forms.

By the end of this tutorial, you will be able to:
- Understand the roles of Models and Views in ASP.NET MVC
- Create and manipulate Models
- Use Views to display data
- Update data using forms

Prerequisites:
- Basic knowledge of C# programming
- Familiarity with ASP.NET MVC architecture

2. Step-by-Step Guide

Models

Models in ASP.NET MVC are responsible for handling the logic for data access and storage. A model is basically a C# class with properties that represent a data table in the database.

Creating a Model

Let's create a simple model for a Student.

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

Each property of the Student model class represents a column of the Student table in the database.

Views

Views in an ASP.NET MVC application are responsible for rendering the user interface (UI). They generate HTML to be sent to the client's browser.

Creating a View

Assume we have a view that will display the details of a Student.

@model Student

<div>
    <h2>@Model.Name</h2>
    <p>Age: @Model.Age</p>
</div>

In this view, @model Student tells the view which model it should be using. @Model.Name and @Model.Age are accessing the properties of the Student model.

Updating Data Using Forms

In ASP.NET MVC, we can use forms to collect and update data.

Here is a simple form that can be used to update the details of a Student.

@model Student

<form method="post" action="UpdateStudent">
    <input type="hidden" name="Id" value="@Model.Id" />
    <input type="text" name="Name" value="@Model.Name" />
    <input type="number" name="Age" value="@Model.Age" />
    <input type="submit" value="Update" />
</form>

This form uses a POST method to send the updated student data to the UpdateStudent action.

3. Code Examples

Let's go through a practical example.

Creating a Model

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}
  • This code snippet creates a Product model with properties Id, Name, and Price.

Creating a View

@model Product

<div>
    <h2>@Model.Name</h2>
    <p>Price: @Model.Price</p>
</div>
  • This view displays the Name and Price of a Product.

Updating Data Using Forms

@model Product

<form method="post" action="UpdateProduct">
    <input type="hidden" name="Id" value="@Model.Id" />
    <input type="text" name="Name" value="@Model.Name" />
    <input type="number" name="Price" value="@Model.Price" />
    <input type="submit" value="Update" />
</form>
  • This form collects the updated Product details and sends them to the UpdateProduct action.

4. Summary

In this tutorial, we've learned about the roles of Models and Views in ASP.NET MVC, created and manipulated Models, used Views to display data, and updated data using forms.

Next, you should learn about Controllers in ASP.NET MVC, as they act as an interface between Models and Views.

Additional resources:

5. Practice Exercises

  1. Create a Model for an Employee with properties Id, Name, Position, and Salary.
  2. Create a View to display the Employee details.
  3. Create a form to update the Employee details.

Solutions:

  1. Employee Model:
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public decimal Salary { get; set; }
}
  1. Employee View:
@model Employee

<div>
    <h2>@Model.Name</h2>
    <p>Position: @Model.Position</p>
    <p>Salary: @Model.Salary</p>
</div>
  1. Update Employee Form:
@model Employee

<form method="post" action="UpdateEmployee">
    <input type="hidden" name="Id" value="@Model.Id" />
    <input type="text" name="Name" value="@Model.Name" />
    <input type="text" name="Position" value="@Model.Position" />
    <input type="number" name="Salary" value="@Model.Salary" />
    <input type="submit" value="Update" />
</form>

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help