Part 29 - Using data transfer object as the model in mvc
2016-10-24 11:05
656 查看
In this video we will discuss, using data transfer object as the model in mvc. Please watch
Part 28, before proceeding.
Let's say the business requirement is such that, we want to display total number of employees by department as shown below. At the moment, either the Employee orDepartment class
does not have Total property. This is one example, where a Data Transfer Object can be used as a model.
Right click on the "Models" folder and add a class with name="DepartmentTotals.cs". Copy and paste the following code.
public class DepartmentTotals
{
public string Name { get; set; }
public int Total { get; set; }
}
Now add the following "EmployeesByDepartment" controller action method toEmployeeController class.
public ActionResult EmployeesByDepartment()
{
var departmentTotals = db.Employees.Include("Department")
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList();
return View(departmentTotals);
}
At this point, build the solution, so that the newly added DepartmentTotals class is compiled.
Now right click on "EmployeesByDepartment" action method in "EmployeeController"and select "Add View" from the context menu.
View name = EmployeesByDepartment
View engine = Razor
Select "Create a strongly-typed view" checkbox
Model class = DepartmentTotals
Model class = DepartmentTotals
To list the employees in ascending order of total employee, use OrderBy() LINQ method as shown below.
var departmentTotals = db.Employees.Include("Department")
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList().OrderBy(y => y.Total);
To sort the list in descending order use, OrderByDescending() LINQ method.
var departmentTotals = db.Employees.Include("Department")
aab8
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList().OrderByDescending(y => y.Total);
return View(departmentTotals);
Part 28, before proceeding.
Let's say the business requirement is such that, we want to display total number of employees by department as shown below. At the moment, either the Employee orDepartment class
does not have Total property. This is one example, where a Data Transfer Object can be used as a model.
Right click on the "Models" folder and add a class with name="DepartmentTotals.cs". Copy and paste the following code.
public class DepartmentTotals
{
public string Name { get; set; }
public int Total { get; set; }
}
Now add the following "EmployeesByDepartment" controller action method toEmployeeController class.
public ActionResult EmployeesByDepartment()
{
var departmentTotals = db.Employees.Include("Department")
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList();
return View(departmentTotals);
}
At this point, build the solution, so that the newly added DepartmentTotals class is compiled.
Now right click on "EmployeesByDepartment" action method in "EmployeeController"and select "Add View" from the context menu.
View name = EmployeesByDepartment
View engine = Razor
Select "Create a strongly-typed view" checkbox
Model class = DepartmentTotals
Model class = DepartmentTotals
To list the employees in ascending order of total employee, use OrderBy() LINQ method as shown below.
var departmentTotals = db.Employees.Include("Department")
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList().OrderBy(y => y.Total);
To sort the list in descending order use, OrderByDescending() LINQ method.
var departmentTotals = db.Employees.Include("Department")
aab8
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList().OrderByDescending(y => y.Total);
return View(departmentTotals);
相关文章推荐
- Using the SharePoint 2010 Client Object Model_Part_6
- Using the SharePoint 2010 Client Object Model_part_4
- Using the SharePoint 2010 Client Object Model_part_5
- Using the SharePoint 2010 Client Object Model_part_1
- Using the SharePoint 2010 Client Object Model_part_1
- Using the SharePoint 2010 Client Object Model_part_2
- Using the SharePoint 2010 Client Object Model_part_2
- Using the SharePoint 2010 Client Object Model_part_3
- Using the SharePoint 2010 Client Object Model_part_3
- Using the SharePoint 2010 Client Object Model_Part_6
- Using the HTMLControl Class in Adobe AIR to parse HTML as a data source
- Using the SharePoint 2010 Client Object Model_part_1
- Using the SharePoint 2010 Client Object Model_part_5
- Using the SharePoint 2010 Client Object Model_part_2
- Using the SharePoint 2010 Client Object Model_part_4
- Using the Message Class---Data Transfer in Service Contracts
- Using the SharePoint 2010 Client Object Model_part_5
- Using the SharePoint 2010 Client Object Model_part_4
- Using the SharePoint 2010 Client Object Model_part_3
- Using the SharePoint 2010 Client Object Model_Part_6