From 7433bb0d135b6889fee1ee1ffc0baa613dacfac7 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Fri, 11 Jun 2021 15:44:31 +1000 Subject: [PATCH] Finished 'ViewModel in ASP.NET MVC' --- .../Controllers/EmployeeController.cs | 48 +++++++++++++++++++ FirstMVCDemo/Controllers/HomeController.cs | 24 ---------- FirstMVCDemo/FirstMVCDemo.csproj | 7 +-- FirstMVCDemo/Models/Address.cs | 16 +++++++ FirstMVCDemo/Models/Employee.cs | 4 +- FirstMVCDemo/Models/EmployeeBusinessLayer.cs | 27 ----------- .../ViewModels/EmployeeDetailsViewModel.cs | 17 +++++++ FirstMVCDemo/Views/Employee/Details.cshtml | 45 +++++++++++++++++ FirstMVCDemo/Views/Home/Index.cshtml | 43 ----------------- 9 files changed, 132 insertions(+), 99 deletions(-) create mode 100644 FirstMVCDemo/Controllers/EmployeeController.cs delete mode 100644 FirstMVCDemo/Controllers/HomeController.cs create mode 100644 FirstMVCDemo/Models/Address.cs delete mode 100644 FirstMVCDemo/Models/EmployeeBusinessLayer.cs create mode 100644 FirstMVCDemo/ViewModels/EmployeeDetailsViewModel.cs create mode 100644 FirstMVCDemo/Views/Employee/Details.cshtml delete mode 100644 FirstMVCDemo/Views/Home/Index.cshtml diff --git a/FirstMVCDemo/Controllers/EmployeeController.cs b/FirstMVCDemo/Controllers/EmployeeController.cs new file mode 100644 index 0000000..1c6dff0 --- /dev/null +++ b/FirstMVCDemo/Controllers/EmployeeController.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +using FirstMVCDemo.Models; +using FirstMVCDemo.ViewModels; + +namespace FirstMVCDemo.Controllers +{ + public class EmployeeController : Controller + { + // GET: Employee + public ActionResult Details() + { + //Employee Basic Details + Employee employee = new Employee() + { + EmployeeId = 101, + Name = "Dillip", + Gender = "Male", + Department = "IT", + Salary = 10000, + AddressId = 1001 + }; + //Employee Address + Address address = new Address() + { + AddressId = 1001, + City = "Bhubaneswar", + State = "Odisha", + Country = "India", + Pin = "755019" + }; + //Creating the View model + EmployeeDetailsViewModel employeeDetailsViewModel = new EmployeeDetailsViewModel() + { + Employee = employee, + Address = address, + PageTitle = "Employee Details Page", + PageHeader = "Employee Details", + }; + //Pass the employeeDetailsViewModel to the view + return View(employeeDetailsViewModel); + } + } +} \ No newline at end of file diff --git a/FirstMVCDemo/Controllers/HomeController.cs b/FirstMVCDemo/Controllers/HomeController.cs deleted file mode 100644 index a0f58ae..0000000 --- a/FirstMVCDemo/Controllers/HomeController.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; - -using FirstMVCDemo.Models; - -namespace FirstMVCDemo.Controllers -{ - public class HomeController : Controller - { - // GET: Home - public ActionResult Index() - { - EmployeeBusinessLayer employeeBL = new EmployeeBusinessLayer(); - Employee employee = employeeBL.GetEmployeeDetails(101); - - ViewBag.Header = "Employee Details"; - - return View(employee); - } - } -} \ No newline at end of file diff --git a/FirstMVCDemo/FirstMVCDemo.csproj b/FirstMVCDemo/FirstMVCDemo.csproj index 97c55c5..e4e4548 100644 --- a/FirstMVCDemo/FirstMVCDemo.csproj +++ b/FirstMVCDemo/FirstMVCDemo.csproj @@ -93,18 +93,19 @@ - + Global.asax + - + - + Web.config diff --git a/FirstMVCDemo/Models/Address.cs b/FirstMVCDemo/Models/Address.cs new file mode 100644 index 0000000..286d4bc --- /dev/null +++ b/FirstMVCDemo/Models/Address.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace FirstMVCDemo.Models +{ + public class Address + { + public int AddressId { get; set; } + public string Country { get; set; } + public string State { get; set; } + public string City { get; set; } + public string Pin { get; set; } + } +} \ No newline at end of file diff --git a/FirstMVCDemo/Models/Employee.cs b/FirstMVCDemo/Models/Employee.cs index 776663a..53cca9d 100644 --- a/FirstMVCDemo/Models/Employee.cs +++ b/FirstMVCDemo/Models/Employee.cs @@ -9,9 +9,9 @@ namespace FirstMVCDemo.Models { public int EmployeeId { get; set; } public string Name { get; set; } - public string Address { get; set; } - public string City { get; set; } public string Gender { get; set; } + public string Department { get; set; } public decimal Salary { get; set; } + public int AddressId { get; set; } } } \ No newline at end of file diff --git a/FirstMVCDemo/Models/EmployeeBusinessLayer.cs b/FirstMVCDemo/Models/EmployeeBusinessLayer.cs deleted file mode 100644 index 27ab66d..0000000 --- a/FirstMVCDemo/Models/EmployeeBusinessLayer.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace FirstMVCDemo.Models -{ - public class EmployeeBusinessLayer - { - public Employee GetEmployeeDetails(int EmployeeId) - { - //Here we hardcoded the data - //later we will discuss how to retrieve - //the data from a database - Employee employee = new Employee() - { - EmployeeId = EmployeeId, - Name = "Pranaya", - Gender = "Male", - City = "Mumbai", - Salary = 1000, - Address = "Andheri" - }; - return employee; - } - } -} \ No newline at end of file diff --git a/FirstMVCDemo/ViewModels/EmployeeDetailsViewModel.cs b/FirstMVCDemo/ViewModels/EmployeeDetailsViewModel.cs new file mode 100644 index 0000000..99052a3 --- /dev/null +++ b/FirstMVCDemo/ViewModels/EmployeeDetailsViewModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +using FirstMVCDemo.Models; + +namespace FirstMVCDemo.ViewModels +{ + public class EmployeeDetailsViewModel + { + public Employee Employee { get; set; } + public Address Address { get; set; } + public string PageTitle { get; set; } + public string PageHeader { get; set; } + } +} \ No newline at end of file diff --git a/FirstMVCDemo/Views/Employee/Details.cshtml b/FirstMVCDemo/Views/Employee/Details.cshtml new file mode 100644 index 0000000..210b686 --- /dev/null +++ b/FirstMVCDemo/Views/Employee/Details.cshtml @@ -0,0 +1,45 @@ +@model FirstMVCDemo.ViewModels.EmployeeDetailsViewModel + +@{ + Layout = null; +} + + + + + + + @Model.PageTitle + + +

@Model.PageHeader

+
+ EmployeeID: @Model.Employee.EmployeeId +
+
+ Name : @Model.Employee.Name +
+
+ Gender : @Model.Employee.Gender +
+
+ Department : @Model.Employee.Department +
+
+ Salary : @Model.Employee.Salary +
+

Employee Address

+
+ City : @Model.Address.City +
+
+ State : @Model.Address.State +
+
+ Country : @Model.Address.Country +
+
+ Pin : @Model.Address.Pin +
+ + diff --git a/FirstMVCDemo/Views/Home/Index.cshtml b/FirstMVCDemo/Views/Home/Index.cshtml deleted file mode 100644 index d9d0d8a..0000000 --- a/FirstMVCDemo/Views/Home/Index.cshtml +++ /dev/null @@ -1,43 +0,0 @@ - -@{ - Layout = null; -} -@model FirstMVCDemo.Models.Employee - - - - - - Page Title - - - -

@ViewBag.Header

- - - - - - - - - - - - - - - - - - - - - - - - - -
Employee ID:@Model.EmployeeId
Name:@Model.Name
Gender:@Model.Gender
City:@Model.City
Salary:@Model.Salary
Address:@Model.Address
- -