Visual Studio and take a ASP.NET Core Web Application


1. Open Visual Studio and take a ASP.NET Core Web Application without Authentication and Uncheck the Https
2. Open Server Explorer(View=> Server Explorer)=> Connect Database=> Server Name- (localdb)\mssqllocaldb Database namemaster
=> Ok
3. Right Click on the master database=> Create a new query=> Write the below code & run to create database & table
Use master
Create Database R39Exam09DB
Go
Use R39Exam09DB
Create Table Trainee
(
TraineeID int primary key identity,
Name varchar (20) null,
CourseName varchar (20) null,
CellPhoneNo varchar (15) null,
Address varchar (50) null
)
Go
Insert into Trainee Values
('Akib', 'C#', '022222', 'Chittagong'),
('Mahfuz', 'WDPF', '055555', 'Dhaka'),
('Rasel', 'NT', '088888', 'Comilla')
Go
Select * from Trainee
Go
This script file save in the project directory.
4. Open Package Manager Console=> Write & Execute the following command to generate model class from the database
Scaffold-DbContext "Server=(localdb)\mssqllocaldb; Database= R39Exam09DB; Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
5. Create Two folders in the root of the project named as- ViewModels and Mapper
6. Take a class in the ViewModels folder named as- TraineeVM
7. Copy the all properties from the auto generated “Trainee” class and paste it in the TraineeVM class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace R39Exam09_Nvit.ViewModels
{
public class TraineeVM
{
public int TraineeId { get; set; }
public string Name { get; set; }
public string CourseName { get; set; }
public string CellPhoneNo { get; set; }
public string Address { get; set; }
}
}
8. Goto NugetPackage Manager and install AutoMapper
9. Goto NugetPackage Manager and install AutoMapper.Extensions.Microsoft.Dependencyinjection

10. Create a class in the Mapper folder name: MappingProfile
= Add namespase: using AutoMapper; and inherit Profile and add code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using R39Exam09_Nvit.Models;
using R39Exam09_Nvit.ViewModels;
namespace R39Exam09_Nvit.Mapper
{
public class MappingProfile: Profile
{
public MappingProfile()
{
CreateMap();
CreateMap()
.ForMember(v => v.TraineeId, opt => opt.Ignore());
}
}
}
= Rebuild
11. Add AutoMapper Service in the ConfigureServices method on Startup class file as like as below---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using R39Exam09_Nvit.Models;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using R39Exam09_Nvit.Mapper;
using R39Exam09_Nvit.ViewModels;
namespace R39Exam09_Nvit
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
Page 3 of 11
public void ConfigureServices(IServiceCollection services)
{
services.Configure(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Auto Mapper Configurations
//var mappingConfig = new MapperConfiguration(mc =>
//{
// mc.AddProfile(new MappingProfile());
//});
//IMapper mapper = mappingConfig.CreateMapper();
//services.AddSingleton(mapper);
services.AddAutoMapper(); //Package=> AutoMapper.Extensions.Microsoft.Dependencyinjection
var connection =
@"Server=(localdb)\mssqllocaldb;Database=R39Exam09DB;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext(options => options.UseSqlServer(connection));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
12. Create an Empty MVC controller named as TraineeController and add the following code to implement CRUD operation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
Page 4 of 11
using Microsoft.EntityFrameworkCore;
using R39Exam09_Nvit.Models;
using AutoMapper;
using R39Exam09_Nvit.ViewModels;
namespace R39Exam09_Nvit.Controllers
{
public class TraineeController : Controller
{
private readonly R39Exam09DBContext _context;
private readonly IMapper _mapper;
public TraineeController(R39Exam09DBContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
[HttpGet]
public async Task Index()
{
return View(await _context.Trainee.ToListAsync());
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create(TraineeVM traineeVM)
{
if (ModelState.IsValid)
{
var trn = _mapper.Map(traineeVM);
_context.Trainee.Add(trn);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(traineeVM);
}
[HttpGet]
public async Task Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var traineeVM = new TraineeVM();
{
Trainee trainee = await _context.Trainee.SingleOrDefaultAsync(c => c.TraineeId == id);
if (trainee == null)
{
return NotFound();
}
Page 5 of 11
traineeVM.TraineeId = trainee.TraineeId;
traineeVM.Name = trainee.Name;
traineeVM.CourseName = trainee.CourseName;
traineeVM.CellPhoneNo = trainee.CellPhoneNo;
traineeVM.Address = trainee.Address;
}
return View(traineeVM);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Edit(TraineeVM traineeVM)
{
if (ModelState.IsValid)
{
var trainee = await _context.Trainee.FindAsync(traineeVM.TraineeId);
if (trainee == null)
{
return NotFound();
}
_mapper.Map(traineeVM, trainee);
_context.Entry(trainee).State = EntityState.Modified;
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(traineeVM);
}
[HttpGet]
public async Task Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var traineeVM = new TraineeVM();
{
Trainee trainee = await _context.Trainee.SingleOrDefaultAsync(c => c.TraineeId == id);
if (trainee == null)
{
return NotFound();
}
traineeVM.TraineeId = trainee.TraineeId;
traineeVM.Name = trainee.Name;
traineeVM.CourseName = trainee.CourseName;
traineeVM.CellPhoneNo = trainee.CellPhoneNo;
traineeVM.Address = trainee.Address;
}
return View(traineeVM);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task DeleteConfirmed(int id, TraineeVM traineeVM)
{
if (ModelState.IsValid)
{
var trainee = await _context.Trainee.FindAsync(traineeVM.TraineeId);
if (trainee == null)

{
return NotFound();
}
var trn = _mapper.Map(traineeVM, trainee);
_context.Trainee.Remove(trn);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(traineeVM);
}
[HttpGet]
public async Task Details(int? id)
{
if (id == null)
{
return NotFound();
}
var traineeVM = new TraineeVM();
{
Trainee trainee = await _context.Trainee.SingleOrDefaultAsync(c => c.TraineeId == id);
if (trainee == null)
{
return NoContent();
}
traineeVM.TraineeId = trainee.TraineeId;
traineeVM.Name = trainee.Name;
traineeVM.CourseName = trainee.CourseName;
traineeVM.CellPhoneNo = trainee.CellPhoneNo;
traineeVM.Address = trainee.Address;
}
return View(traineeVM);
}
}
}
13. Create All of the View Pages for each of the action method to implement CRUD Operation
CRUD OPERATION COMPLETED
14. Open “_Layout.cshtml” file from the Views=> Shared directory & also add a menu for Trainee controller as like as below----
…………………………………
…………………………………


Home

Privacy


Trainee
Information


…………………………………
…………………………………
Implement Data Validation

15. Open View Model “TraineeVM” class file and add some of the code to implement validation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace R39Exam09_Nvit.ViewModels
{
public class TraineeVM
{
public int TraineeId { get; set; }
[Required]
[StringLength(20, MinimumLength = 2)]
[Display(Name = "Trainee Name")]
public string Name { get; set; }
[Required]
[StringLength(20, MinimumLength = 2)]
[Display(Name = "Course Name")]
public string CourseName { get; set; }
[Required]
[StringLength(11)]
[Display(Name = "Cell Phone No.")]
public string CellPhoneNo { get; set; }
[Required]
public string Address { get; set; }
}
}
REBUILD=> Run Application=> DATA VALIDATION COMPLETED
Implement Partial View
16. Create a Partial page named as “_MyPartial” in the Views=>Shared folder Page as like as below---

Trainee Information (I'm Response Partial Page!!!!!!)


17. Now, Call it on the Index.cshtml Page of Trainee as Like that
@model IEnumerable
@{
ViewData["Title"] = "Index";
}

@Html.Partial("~/Views/Shared/_MyPartial.cshtml");


Create New

………………………….
………………………….
PARTIAL VIEW COMPLETED
Routing Implementation

18. Open “Stratup.cs” file and add custom conventional routing as like as below---
……………………………
……………………………
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Nvit",
template: "{controller=Trainee}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
…………………………..
…………………………..
19. Open “TraineeController.cs” file and add the attribute routing on the “Index” Action Method as like as below----
…………………………..
…………………………..
[Route("TraineeInfo")]
[HttpGet]
public async Task Index()
{
return View(await _context.Trainee.ToListAsync());
}
…………………………..
…………………………..
ROUTING COMPLETED
Implement Filtering, Sorting & Pagination
20. Create a Class on Project Root Named as “PaginatedList” for Pagination. The Code of this file as like as below----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace R39Exam09_Nvit
{
public class PaginatedList : List
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(List items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}

}
public bool HasNextPage
{
get
{
return (PageIndex < TotalPages);
}
}
public static async Task> CreateAsync(IQueryable source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList(items, count, pageIndex, pageSize);
}
}
}
21. Modify the Index Action Method in TraineeController as like as below---
[Route("TraineeInfo")]
[HttpGet]
public async Task Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
if (searchString != null)
{
pageNumber = 1;
}
else
{
searchString = currentFilter;
}
ViewData["CurrentFilter"] = searchString;
var trainees = from s in _context.Trainee
select s;
if (!String.IsNullOrEmpty(searchString))
{
trainees = trainees.Where(s => s.Name.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
trainees = trainees.OrderByDescending(s => s.Name);
break;
default:
trainees = trainees.OrderBy(s => s.Name);
break;
}
int pageSize = 3;
return View(await PaginatedList.CreateAsync(trainees.AsNoTracking(), pageNumber ?? 1, pageSize));
}
22. Modify the Index.cshtml file of TraineeController as like as below------------
Page 10 of 11
@model PaginatedList
@{
ViewData["Title"] = "Index";
}

@Html.Partial("~/Views/Shared/_MyPartial.cshtml")


Create New




Find by name:


Back to Full List







Trainee Name














@foreach (var item in Model)
{


@Html.DisplayFor(modelItem => item.Name)


@Html.DisplayFor(modelItem => item.CourseName)


@Html.DisplayFor(modelItem => item.CellPhoneNo)


@Html.DisplayFor(modelItem => item.Address)



Edit |
Details |
Delete


}


@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-pageNumber="@(Model.PageIndex - 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @prevDisabled">
Previous

asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-pageNumber="@(Model.PageIndex + 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @nextDisabled">
Next

23. Rebuild=> Run Application=> Enjoy Your CRUD Operation with The Following Features-----
ASP.NET MVC Core Application, Database First Approach, View Model, Auto Mapper, Dependency Injection,
Routing(Conventional & Attribute), Filtering, Sorting, Pagination, Data Validation, Menu, Partial Page, Bootstrap and many
more……..
Project Structure as Like as Below---

buy my photos... sell photo

Check out my photo on dreamstime https://www.dreamstime.com/stock-photo-dewdrop-leaf-morning-sun-rise-dew-drops-wash-tree-image82688192 #dreamstimeapp