filmov
tv
Part 29 Using data transfer object as the model in mvc

Показать описание
In this video we will discuss, using data transfer object as the model in mvc. Please watch Part 28, before proceeding.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Let's say the business requirement is such that, we want to display total number of employees by department. At the moment, either the Employee or Department class does not have Total property. This is one example, where a Data Transfer Object can be used as a model.
public class DepartmentTotals
{
public string Name { get; set; }
public int Total { get; set; }
}
Now add the following "EmployeesByDepartment" controller action method to EmployeeController 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")
.GroupBy(x =] x.Department.Name)
.Select(y =] new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList().OrderByDescending(y =] y.Total);
return View(departmentTotals);
Text version of the video
Slides
All ASP .NET MVC Text Articles
All ASP .NET MVC Slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Let's say the business requirement is such that, we want to display total number of employees by department. At the moment, either the Employee or Department class does not have Total property. This is one example, where a Data Transfer Object can be used as a model.
public class DepartmentTotals
{
public string Name { get; set; }
public int Total { get; set; }
}
Now add the following "EmployeesByDepartment" controller action method to EmployeeController 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")
.GroupBy(x =] x.Department.Name)
.Select(y =] new DepartmentTotals
{
Name = y.Key, Total = y.Count()
}).ToList().OrderByDescending(y =] y.Total);
return View(departmentTotals);
Text version of the video
Slides
All ASP .NET MVC Text Articles
All ASP .NET MVC Slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
Комментарии