Web
Analytics
viewmodel in mvc example | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Employee Model Class

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VMEx.Models
{
    public class Employee
    {
        public int empid { get; set; }
        public string Employeename { get; set; }
        public string empfathername { get; set; }

        public int salary { get; set; }
    }

    public class EmployeeVM
    {
        public string Employeename { get; set; }
        public string empfathername { get; set; }

        public int salary { get; set; }
        public string salarycolor { get; set; }
    }
}




 Home Controller

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VMEx.Models;

namespace VMEx.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            List<Employee> empobj = new List<Models.Employee>
            {
                new Employee { empid=1, empfathername="sharma" , Employeename="yogesh" , salary=1000 },
                                new Employee { empid=2, empfathername="rathore" , Employeename="vikram" , salary=5000 }
            };
            //return View("Index", empobj);

            List<EmployeeVM> empvmobj = new List<EmployeeVM>();
            foreach(Employee emp in empobj)
            {
                EmployeeVM evm = new Models.EmployeeVM();
                evm.Employeename = emp.Employeename;
                evm.empfathername = emp.empfathername;
                evm.salary = emp.salary;
                if (emp.salary>1000)
                {
                    evm.salarycolor = "Red";
                }
                else
                {
                    evm.salarycolor = "green";
                }
                empvmobj.Add(evm);

            }
            return View("EmployeeVMView", empvmobj);
        }
    }
}

 Index.cshtml

 

@model IEnumerable<VMEx.Models.Employee>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.empid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Employeename)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.empfathername)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.salary)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.empid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Employeename)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.empfathername)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.salary)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>





EmployeeVMView.cshtml

 

@model IEnumerable<VMEx.Models.EmployeeVM>

@{
    ViewBag.Title = "EmployeeVMView";
}

<h2>EmployeeVMView</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Employeename)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.empfathername)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.salary)
        </th>
       
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Employeename)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.empfathername)
        </td>
        <td>
            <span style="background-color:@item.salarycolor">@item.salary</span>
                
            @*@Html.DisplayFor(modelItem => item.salary)*@
        </td>
       
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

vmex