Finished 'ViewModel in ASP.NET MVC'

This commit is contained in:
jason.zhu 2021-06-11 15:44:31 +10:00
parent 74036dd9de
commit 7433bb0d13
9 changed files with 132 additions and 99 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -93,18 +93,19 @@
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\EmployeeController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\Address.cs" />
<Compile Include="Models\Employee.cs" />
<Compile Include="Models\EmployeeBusinessLayer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\EmployeeDetailsViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\web.config" />
<None Include="packages.config" />
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Employee\Details.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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;
}
}
}

View File

@ -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; }
}
}

View File

@ -0,0 +1,45 @@
@model FirstMVCDemo.ViewModels.EmployeeDetailsViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@Model.PageTitle</title>
</head>
<body>
<h1>@Model.PageHeader</h1>
<div>
EmployeeID: @Model.Employee.EmployeeId
</div>
<div>
Name : @Model.Employee.Name
</div>
<div>
Gender : @Model.Employee.Gender
</div>
<div>
Department : @Model.Employee.Department
</div>
<div>
Salary : @Model.Employee.Salary
</div>
<h1>Employee Address</h1>
<div>
City : @Model.Address.City
</div>
<div>
State : @Model.Address.State
</div>
<div>
Country : @Model.Address.Country
</div>
<div>
Pin : @Model.Address.Pin
</div>
</body>
</html>

View File

@ -1,43 +0,0 @@

@{
Layout = null;
}
@model FirstMVCDemo.Models.Employee
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Page Title</title>
</head>
<body>
<h2>@ViewBag.Header</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>
<tr>
<td>Salary:</td>
<td>@Model.Salary</td>
</tr>
<tr>
<td>Address:</td>
<td>@Model.Address</td>
</tr>
</table>
</body>
</html>