Click here to Skip to main content
14,695,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to veew report as following. How can I do it by using ViewModel And Save this information another table. Please help me.Especially i can't show sum of salary and Designation
Name Code Designaton Total Salary
Shihab CSE01 CEO 500000



My Model Class are:

public class Employee
    {
        public int EmployeeId { get; set; }

        [Display(Name = "Employee Code")]
        public string EmployeeCode { get; set; }
        public virtual List<EmployeeDesignation> EmployeeDesignationList { get; set; }
        public virtual List<EmployeeSalary> EmployeeSalaryList { get; set; }
        public virtual List<EmployeePay> PayList { get; set; }
}

public class EmployeeDesignation
    {
        public int EmployeeDesignationId { get; set; }
        public int EmployeeId { get; set; }
        public Employee AnEmploye { get; set; }
        public int DesignationId { get; set; }
        public virtual List< Designation > DesignationObj { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndtDate { get; set; }
    }
public class Designation
    {
        public int DesignationId { get; set; }
        public string DesignationName { get; set; }
    }

public class EmployeeSalary
    {
        public int EmployeeSalaryId { get; set; }
        public double BasicSalary { get; set; }
        public double MedicalAnnouncement { get; set; }
        public double HouseRent { get; set; }
        public int EmployeeId { get; set; }
        public virtual Employee AnEmployee { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndtDate { get; set; }
}


My ViewModel is:
public class EmployeeViewModel
    {
        public List<Employee> Employees { get; set; }
        public List<EmployeeDesignation> Designations { get; set; }
        public List<Designation> DesignationObj { get; set; }
        public List<EmployeeSalary> Salaries { get; set; }
    }



My Controller is:
public ActionResult EmpReport()
        {
            var employees = (from employee in db.Employees
                            select employee).ToList();


            var salaries = (from salary in db.Salaries
                            select salary).ToList();


            var employeeViewModel = new EmployeeViewModel();

            employeeViewModel.Employees = employees;
            employeeViewModel.Designations = db.EmployeeDesignations.Include(x => x.DesignationObj).ToList();
            employeeViewModel.Salaries = db.Salaries.ToList();

            return View(employeeViewModel);
        }


My View Is:
@model EmployeeManagementSystem.ViewModel.EmployeeViewModel

@{
    ViewBag.Title = "EmpReport";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>EmpReport</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeCode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ContactNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DesignationName)
        </th>
       @* <th>
            @Html.DisplayNameFor(model => model.BasicSalary)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MedicalAnnouncement)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HouseRent)
        </th>*@
        <th></th>
    </tr>
   
@foreach (var item in Model.Employees) {
     <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ContactNo)
        </td>
        
         <td>
            @Html.DisplayFor(modelItem => item.EmployeeDesignationList)
        </td>
       <td>
            @Html.DisplayFor(modelItem => item.BasicSalary)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MedicalAnnouncement)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HouseRent)
        </td>
   </tr>
}
        @foreach (var item in Model.Designations)
        { 
          <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DesignationObj.DesignationName)
        </td>
            </tr>   
        }
          

</table>
Posted
Updated 11-Mar-14 5:55am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900