| by Achyut Kendre | No comments

What is ViewModel in ASP.NET Core?

ASP.NET Core 3.1
View Model In ASP.NET Core

View Model is dummy model that contains public properties those need to be represented in a View. View Model will be crated only for view. We can say it is just used to carry data from action to view or view back to action. View Model can be used in following situations –

  1. If you want to communicate data from multiple models.
  2. If you want to communicate a part of entire model
  3. If you want to communicate a calculated result.

e.g. View Model Example – in this example we will create three models called customer, emp and product. We will use view model to communicate the some data from customer model, emp model and product model.

Step 1: Create a ASP.NET Core empty project.
Step 2: Create customer model in models folder as follows

namespace ViewModelExample.Models
{
    public class Customer
    {
        public Int64 CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string EmailID { get; set; }
        public string MobileNo { get; set; }
        public string Address { get; set; }
        public decimal CreditLimit { get; set; }

        public Customer()
        {
            this.CustomerID = 121;
            this.CustomerName = "Nilesh";
            this.EmailID = "Nilesh@hotmail.com";
            this.MobileNo = "987998989";
            this.Address = "Nigdi Pune";
            this.CreditLimit = 56000;
        }
    }
}

Step 3: Create emp model in models folder as follows –

namespace ViewModelExample.Models
{
    public class Emp
    {
        public Int64 EmpID { get; set; }
        public string EmpName { get; set; }
        public string DeptName { get; set; }
        public decimal Salary { get; set; }
        public Emp()
        {
            this.EmpID = 121;
            this.EmpName = "Akash";
            this.DeptName = "Computer";
            this.Salary = 56000;
        }
    }
}

Step 4 : Implement a one more model called product as follows –

namespace ViewModelExample.Models
{
    public class Product
    {
        public Int64 ProductID { get; set; }
        public string ProductName { get; set; }
        public string MfgName { get; set; }
        public decimal Price { get; set; }
        public Product()
        {
            this.ProductID = 123;
            this.ProductName = "Mouse";
            this.MfgName = "Intex";
            this.Price = 450;
        }
    }
}

Step 5: Create ViewModels folder and create a view model to communicate some data from customer, some from emp and some from product as follows –

public class CustEmpProdVM
    {
        public string CName { get; set; }
        public string MobileNo { get; set; }
        public decimal CrLimit { get; set; }
        public string EName { get; set; }
        public decimal Salary { get; set; }
        public string PName { get; set; }
        public decimal Price { get; set; }
    }

Step 6: Create a HomeController and Index action as follows –

namespace ViewModelExample.Cotnrollers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            Customer c = new Customer();
            Emp e = new Emp();
            Product p = new Product();
            CustEmpProdVM rec = new CustEmpProdVM();
            rec.CName = c.CustomerName;
            rec.MobileNo = c.MobileNo;
            rec.CrLimit = c.CreditLimit;
            rec.EName = e.EmpName;
            rec.Salary = e.Salary;
            rec.Price = p.Price;
            rec.PName = p.ProductName;
            return View(rec);
        }
    }
}

Step 7: Create a strongly typed Index view to display this combined data to a view as follows –

@model ViewModelExample.ViewModels.CustEmpProdVM
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
     <h1> Index action of home controlle!</h1>
    Customer Name:@Model.CName <br />
    Mobile No: @Model.MobileNo <br />
    Credit Limit:@Model.CrLimit <br />
    Emp Name:@Model.EName <br />
    Salary :@Model.Salary <br />
    Product Name:@Model.PName <br />
    Price :@Model.Price <br />
</body>
</html>

This is about view model.