Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 29 - Using data transfer object as the model in mvc

Suggested Videos 
Part 26 - Customizing the auto-generated index view
Part 27 - Customizing the auto-generated create view
Part 28 - Customizing the auto-generated edit view



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 or Department class does not have Total property. This is one example, where a Data Transfer Object can be used as a model.
Employees By Department



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

5 comments:

  1. Hello Sir G
    As i have both the version of VS, i.e VS 2010 & VS 2012 in latest version mvc 3&4 are included by default so i think there is no need to install separately mvc 4.
    please tell me mian feature of vs 2012 over vs 2010/
    Dear Sir G you are doing a very good and outstanding job.
    May GOD bless you more and more and.........
    Thank you so much Sir G!!!

    ReplyDelete
  2. Hi Venkat,

    I am getting an error
    like this


    Server Error in '/' Application.

    The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcDemo3.Models.Employee]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcDemo3.Models.DepartmentTotals]'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcDemo3.Models.Employee]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcDemo3.Models.DepartmentTotals]'.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    please help me

    ReplyDelete
    Replies
    1. Try without setting up any data context class

      Delete
  3. Hi Prahim,

    How could I pass to the same View ID of department to be available for making a link:
    @Html.ActionLink(item.Name, "index", new { @id = item.DepId })

    Kind regards

    ReplyDelete
  4. Hello Sir ,i m getting an error while tring to create a DTO usin g code first EF ,its asking for Key ... plzz tell me how can i solve d issues ..plzz sir, upload a video by describing the DTO in details .

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.