mvc core chapter -2
1. repository -- add inumerable<>functioname() --- for looping in view like list in python for each esma a
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);
IEnumerable<Employee> GetAllEmployees();
}
}
2. implementing it in repository and returning whole list because we want to display whole list .
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 IEnumerable<Employee> GetAllEmployees()
{
return _employeeList;
}
public Employee GetEmployee(int id)
{
return _employeeList.FirstOrDefault(e => e.id == id);
}
}
}
3. HomeController -- showing all list in index
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"
};
string s = "alu";
string d = "talu ma";
string name = $"My fullname is {s}{d}";
ViewBag.game = name;
return View(homeDetailViewModel);
}
// 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 ViewResult Index()
{
var model = _employeeRepository.GetAllEmployees();
return View(model);
}
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 });
}
}
}
4. view:
@model IEnumerable<God_Helpme_ConquerGreatness.Models.Employee>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ViewData["Title"]</title>
</head>
<body>
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<table>
<thead>
<tr>
<th>ID </th>
<th>Name </th>
<th>Email </th>
<th>Department</th>
</tr>
</thead>
<tbody>
@foreach (var employee in Model)
{
<tr>
<td>@employee.id</td>
<td>@employee.Name</td>
<th>@employee.Email </th>
<td>@employee.Department</td>
</tr>
}
</tbody>
</table>>
</body>
</html>
Comments
Post a Comment