@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>
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");
}
}
}
@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>
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");
}
}
}
@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 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 refers to a design pattern that divides an application into three distinct layers, each responsible for specific aspects of functionality.
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.