mvc core -chapter 1

 first model of employee :(contains only getters and setters):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;


namespace God_Helpme_ConquerGreatness.Models

{

    public class Employee

    {

        public int id { get; set; }

        public String Name { get; set; }


        public String Email { get; set; }


        public String Department { get; set; }


    }

}


2. Interface(which is a abstraction .. contains only the name of function without a body)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace God_Helpme_ConquerGreatness.Models
{
    public interface IEmployeeRepository
    {
        Employee GetEmployee(int id);

    } 
}

3.Employee repository class (whch inherits interface and also gives body to the function)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace God_Helpme_ConquerGreatness.Models
{

    public class MockEmployeeRepository : IEmployeeRepository
    {
        private List<Employee> _employeeList; //private field of type list 
        public MockEmployeeRepository() //constructor of a class
        {
            _employeeList = new List<Employee>() //remember getter and setters of employee class
            {
            new Employee(){ id=1, Name="Rajan", Department="IT",Email="rajan@centurybank.com.np"},
            new Employee() { id = 2, Name = "Shobhana", Department = "Branding and Communication", Email = "shobhana@centurybank.com.np" },
            new Employee() { id = 3, Name = "Kriti", Department = "Loan", Email = "kriti@centurybank.com.np" }
             };

        }

        public Employee GetEmployee(int id)
        {
        return _employeeList.FirstOrDefault(e => e.id == id);
        }
    }
}

4.viewmodel (which is created to give value for views from the controller)

using God_Helpme_ConquerGreatness.Models;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;


namespace God_Helpme_ConquerGreatness.NewFolder1

{

    public class HomeDetailViewModel

    {

        public Employee employee { get; set; }

        public string pagetitle { get; set; }


    }

}

5. Controller 

using God_Helpme_ConquerGreatness.Models;
using God_Helpme_ConquerGreatness.NewFolder1;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace God_Helpme_ConquerGreatness.Controllers
{
    public class HomeController : Controller
    {
       // private readonly ILogger<HomeController> _logger;

        private readonly IEmployeeRepository _employeeRepository;

        public HomeController(IEmployeeRepository employeeRepository) //injecting interface to the constructor and name this as employeeRepository 
        {
            _employeeRepository = employeeRepository;
        }


        public ViewResult Details() //strongly typed view .. passing model to view
        {
            HomeDetailViewModel homeDetailViewModel = new HomeDetailViewModel()
            {
                employee = _employeeRepository.GetEmployee(1),
                pagetitle = "Employee Details"
            };
            
            return View(homeDetailViewModel);

        }


        // public ViewResult Details()  //using view bag
        //{
        //  Employee eee = _employeeRepository.GetEmployee(1);
        //ViewBag.PageTitle = "Employee Details";
        //return View(eee); //returing object

        //}

        // public ViewResult Details()  //using view bag
        //{
        //  Employee eee = _employeeRepository.GetEmployee(1);
        //ViewBag.Employee = eee;
        //ViewBag.PageTitle = "Employee Details";
        //return View();

        //}

        //  public ViewResult Details() //using view Data
        // {
        //  Employee eee = _employeeRepository.GetEmployee(1);
        // ViewData["Employee"] = eee;
        // ViewData["PageTitle"] = "Employee Details";
        //return View();

        // }

        //public ObjectResult Details() //returing xml or jsonresult
        // {
        //   Employee eee = _employeeRepository.GetEmployee(1);
        //   return new ObjectResult(eee);
        //return _employeeRepository.GetEmployee(1).Name;
        // return Json(new { id = 1, name = "Rajan" }); //anonymyus object
        // }


        // public HomeController(ILogger<HomeController> logger)
        //{
        // _logger = logger;
        //  }

        public IActionResult Index()
        {
            //return ("Hello world");
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        //this process if constructor injection
       

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

6. view




<!--- using view data .. view data is not prefered
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>@ViewData["PageTitle"]</title>

</head>
<body>
    @{
        var employee = ViewData["Employee"] as God_Helpme_ConquerGreatness.Models.Employee;
    }

    <h1>@employee.Name</h1>
    <div>
        Email : @employee.Email
    </div>
    <div>
        Department : @employee.Department
    </div>
</body>

</html>

    -->
<!-- if controller desnt return model we have to mention like this Employee.Name
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>@ViewBag.PageTitle</title>
</head>
<body>
    <!--<h1>@ViewBag.Employee.Name</h1>-->
<!-- <div>Email : @ViewBag.Employee.Email</div>-->
<!--  <div>Department : @ViewBag.Employee.Department</div>-->
</body>
</html>
  
 

<!--enable intelli-sense-->
@model God_Helpme_ConquerGreatness.NewFolder1.HomeDetailViewModel

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>@ViewBag.PageTitle</title>
</head>
<body>
    <h1>@Model.employee.Name</h1>
    <div>Email : @Model.employee.Email</div>
    <div>Department : @Model.employee.Department</div>
</body>
</html>

Comments

Popular Posts