Content

  • Update data using EF Core
  • Delete data using EF Core
  • Layered Architecture
  • 3-Tier Architecture
  • Repository Pattern
  • Project Setup using Layered Architecture

Update data using EF Core

Steps:

  1. Make required changes in "Index" view - pass Id for edit & delete
  2. Create [HttpGet] Edit Method which will receive Id
  3. Find Model from database by using Id
  4. Pass model to View
  5. Add Edit View with Template & Model
  6. Make required changes in View - update Id as hidden
  7. Create [HttpPost] Edit Method which will receive Model to update
  8. Update received model by using Update method

Example

View File: Index

@model IEnumerable<Test.Web.Models.Students>
@{
    ViewData["Title"] = "Index";
}
    
<h1>Index</h1>
    
<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.City)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
                   
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
}
    </tbody>
</table>

Controller File:

using Microsoft.AspNetCore.Mvc;
using Test.Web.Data;
using Test.Web.Models;

namespace Test.Web.Controllers
{
    public class StudentController : Controller
    {
        private readonly ApplicationDbContext _context;

        public StudentController(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
           var students = _context.Students.ToList();
           return View(students);
        }
        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Students student)
        {
            _context.Students.Add(student);
            _context.SaveChanges();

            //after adding new student,redirecting to the student list i.e. Index method
            return RedirectToAction("Index");
        }

        [HttpGet]
        public IActionResult Edit(int id)
        {
            var student = _context.Students.Find(id);
            return View(student);
        }

        [HttpPost]
        public IActionResult Edit(Students student)
        {
            _context.Students.Update(student);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}

View File : Edit

@model Test.Web.Models.Students
@{
    ViewData["Title"] = "Edit";
}

<h1>Edit</h1>

<h4>Students</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                
                <input type="hidden" asp-for="Id" class="form-control" />
                <span asp-validation-for="Id" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="City" class="control-label"></label>
                <input asp-for="City" class="form-control" />
                <span asp-validation-for="City" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Update" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

Delete data using EF Core

Steps:

  1. Create [HttpGet] Delete Method which will receive Id
  2. Find Model from database by using Id
  3. Pass model to View
  4. Add Delete View with Template & Model - to Confirm delete action
  5. Create [HttpPost] Delete Method which will receive Model to delete
  6. Delete received model by using Remove method

Example

Controller File:

using Microsoft.AspNetCore.Mvc;
using Test.Web.Data;
using Test.Web.Models;

namespace Test.Web.Controllers
{
    public class StudentController : Controller
    {
        private readonly ApplicationDbContext _context;

        public StudentController(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
           var students = _context.Students.ToList();
           return View(students);
        }
        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Students student)
        {
            _context.Students.Add(student);
            _context.SaveChanges();

            //after adding new student,redirecting to the student list i.e. Index method
            return RedirectToAction("Index");
        }

        [HttpGet]
        public IActionResult Edit(int id)
        {
            var student = _context.Students.Find(id);
            return View(student);
        }

        [HttpPost]
        public IActionResult Edit(Students student)
        {
            _context.Students.Update(student);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpGet]
        public IActionResult Delete(int id)
        {
            var student = _context.Students.Find(id);
            return View(student);
        }

        [HttpPost]
        public IActionResult Delete(Students student)
        {
            _context.Students.Remove(student);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}

View File: Delete

@model Test.Web.Models.Students
@{
    ViewData["Title"] = "Delete";
}
<h3>Are you sure you want to delete this Student Data?</h3>
<div>
   
    
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Id)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Id)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Name)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Name)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.City)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.City)
        </dd>
    </dl>
    
    <form asp-action="Delete">
        <input type="submit" value="Delete" class="btn btn-danger" /> |
        <a asp-action="Index">Back to List</a>
    </form>
</div>

Layered Architecture

Layered architecture in .NET Core MVC refers to organizing your application's codebase into distinct layers, each responsible for different concerns. This approach promotes modularity, separation of concerns, and maintainability


3-Tier Architecture

3-tier architecture refers to a design pattern that divides an application into three distinct layers, each responsible for specific aspects of functionality.

  • Presentation Layer (UI): This layer is responsible for presenting the user interface to the end-users and handling user interactions.
  • Business Logic Layer (Service Layer): The business logic layer contains the core logic of the application. It encapsulates the business rules, workflows, and operations performed by the application.
  • Data Access Layer (Data Layer): The data access layer is responsible for interacting with the data storage mechanisms, such as databases or external services. It encapsulates the logic for querying, inserting, updating, and deleting data from the underlying database

Repository Pattern

Repository Pattern is used to separate the logic that retrieves data from the underlying data source (such as a database) from the rest of the application,it is intended to create an abstraction layer between the data access layer and the business logic layer of an application.

Steps:

  1. Define your model
  2. Create a repository interface
  3. Implement the repository
  4. Configure Dependency Injection (in Program.cs)
  5. Use the repository in your controllers

Project Setup using Layered Architecture

Steps:

  1. Create Class Library Project to store Models
  2. Create Class Library Project to implement Data Access & Business Logic
  3. Create ASP.NET Core Web App to implement UI
  4. Add project reference of Model project to Data Access Project
  5. Add project reference of Data Access Project to UI Project