In the previous post we
saw on how to setup Entity Framework Core for out Asp.Net Core application. In
this post we will use the EFCore and
create a controller for CRUD operation.
In this sample we will not do too many customization we will rather use the
Built in Scaffolding from Visual Studio to create the controller.
In Visual Studio Right-click on the Controllers folder in Solution Explorer and select Add > Controller.
In Visual Studio Right-click on the Controllers folder in Solution Explorer and select Add > Controller.
Select the option MVC Controller with read/write actions
Next
select the DBContext and a Model class, in this sample we will use the Users
Model, enter a name for the controller UserController and click Add.
Visual Studio will
create us a controller with Action methods for CRUD operations as follows.
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using HelloMVC.Models.EFCoreModel;
namespace HelloMVC.Controllers
{
public class UsersController : Controller
{
private readonly
UserRegistrationContext _context;
public
UsersController(UserRegistrationContext context)
{
_context = context;
}
//
GET: MyUsers
public async Task<IActionResult>
Index()
{
return View(await _context.Users.ToListAsync());
}
//
GET: MyUsers/Details/5
public async Task<IActionResult>
Details(int? id)
{
if (id == null)
{
return NotFound();
}
var users = await _context.Users
.FirstOrDefaultAsync(m =>
m.UserId == id);
if (users == null)
{
return NotFound();
}
return View(users);
}
//
GET: MyUsers/Create
public IActionResult
Create()
{
return View();
}
//
POST: MyUsers/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult>
Create([Bind("UserId,FirstName,LastName,Phone,Email,Address,AttachmentPath")] Users users)
{
if (ModelState.IsValid)
{
_context.Add(users);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(users);
}
//
GET: MyUsers/Edit/5
public async Task<IActionResult>
Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var users = await _context.Users.FindAsync(id);
if (users == null)
{
return NotFound();
}
return View(users);
}
//
POST: MyUsers/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult>
Edit(int id, [Bind("UserId,FirstName,LastName,Phone,Email,Address,AttachmentPath")] Users users)
{
if (id != users.UserId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(users);
await
_context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if
(!UsersExists(users.UserId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(users);
}
//
GET: MyUsers/Delete/5
public async Task<IActionResult>
Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var users = await _context.Users
.FirstOrDefaultAsync(m =>
m.UserId == id);
if (users == null)
{
return NotFound();
}
return View(users);
}
//
POST: MyUsers/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult>
DeleteConfirmed(int id)
{
var users = await _context.Users.FindAsync(id);
_context.Users.Remove(users);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool UsersExists(int id)
{
return _context.Users.Any(e => e.UserId ==
id);
}
}
}
No comments:
Post a Comment