Finished 'ViewModel in ASP.NET MVC'
This commit is contained in:
parent
74036dd9de
commit
7433bb0d13
48
FirstMVCDemo/Controllers/EmployeeController.cs
Normal file
48
FirstMVCDemo/Controllers/EmployeeController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -93,18 +93,19 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="App_Start\RouteConfig.cs" />
|
<Compile Include="App_Start\RouteConfig.cs" />
|
||||||
<Compile Include="Controllers\HomeController.cs" />
|
<Compile Include="Controllers\EmployeeController.cs" />
|
||||||
<Compile Include="Global.asax.cs">
|
<Compile Include="Global.asax.cs">
|
||||||
<DependentUpon>Global.asax</DependentUpon>
|
<DependentUpon>Global.asax</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Models\Address.cs" />
|
||||||
<Compile Include="Models\Employee.cs" />
|
<Compile Include="Models\Employee.cs" />
|
||||||
<Compile Include="Models\EmployeeBusinessLayer.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="ViewModels\EmployeeDetailsViewModel.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Views\web.config" />
|
<Content Include="Views\web.config" />
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
<Content Include="Views\Home\Index.cshtml" />
|
<Content Include="Views\Employee\Details.cshtml" />
|
||||||
<None Include="Web.Debug.config">
|
<None Include="Web.Debug.config">
|
||||||
<DependentUpon>Web.config</DependentUpon>
|
<DependentUpon>Web.config</DependentUpon>
|
||||||
</None>
|
</None>
|
||||||
|
16
FirstMVCDemo/Models/Address.cs
Normal file
16
FirstMVCDemo/Models/Address.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -9,9 +9,9 @@ namespace FirstMVCDemo.Models
|
|||||||
{
|
{
|
||||||
public int EmployeeId { get; set; }
|
public int EmployeeId { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Address { get; set; }
|
|
||||||
public string City { get; set; }
|
|
||||||
public string Gender { get; set; }
|
public string Gender { get; set; }
|
||||||
|
public string Department { get; set; }
|
||||||
public decimal Salary { get; set; }
|
public decimal Salary { get; set; }
|
||||||
|
public int AddressId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
17
FirstMVCDemo/ViewModels/EmployeeDetailsViewModel.cs
Normal file
17
FirstMVCDemo/ViewModels/EmployeeDetailsViewModel.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
45
FirstMVCDemo/Views/Employee/Details.cshtml
Normal file
45
FirstMVCDemo/Views/Employee/Details.cshtml
Normal 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>
|
@ -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>
|
|
Loading…
x
Reference in New Issue
Block a user