using Microsoft.AspNetCore.Mvc;
using Test.Web.Data;
namespace Test.Web.Controllers
{
public class StudentController : Controller
{
private readonly ApplicationDbContext _context;
public StudentController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
}
}
We can select data from database using ToList method
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);
}
}
}
Right click on Method Name -> Add View -> Razor View -> Select Template -> Select Model Class -> Add
@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.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</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");
}
}
}
@model Test.Web.Models.Students
@{
ViewData["Title"] = "Create";
}
<h1>Add New Student</h1>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></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="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
<li class="nav-item">
@* asp-controller- Controller name
asp-action- Action/Method name *@
<a class="nav-link text-dark" asp-area="" asp-controller="Student" asp-action="Index">Student List</a>
</li>