MVC CRUD Sample
First create a solution
File -> New -> Project then select as followed image
Now created solution.Create a table 'TblRegister' on sql server database
Add Entity-framework package to solution using NuGet Packager
Right click Model folder then go to Add->New Item select ADO.NET Entity Data Model
Create Entity Model of our 'CRUD' Database (If need EF model creation tutorial. please visit
https://www.tutorialspoint.com/entity_framework/entity_database_first_approach.htm )
Now db Entity model created like below pic
For Creating 'RegisterController' Right click Controller folder -> Add Controller
and add below code to on RegisterController
CRUDEntities dbEntity = new CRUDEntities();
// GET: Register
public ActionResult Index()
{
var res = dbEntity.TblRegisters.ToList();
return View(res);
}
public ActionResult Create()
{
return View(new TblRegister());
}
[HttpPost]
public ActionResult Create(TblRegister model)
{
dbEntity.TblRegisters.Add(model);
dbEntity.SaveChanges();
return RedirectToAction("index");
}
public ActionResult Edit(int id)
{
TblRegister model = dbEntity.TblRegisters.Where(x => x.Id == id).FirstOrDefault();
return View(model);
}
[HttpPost]
public ActionResult Edit(TblRegister model)
{
TblRegister data = dbEntity.TblRegisters.Where(x => x.Id == model.Id).FirstOrDefault();
data.Name = model.Name;
data.Place = model.Place;
dbEntity.SaveChanges();
return RedirectToAction("index");
}
public ActionResult Details(int id)
{
TblRegister model = dbEntity.TblRegisters.Where(x => x.Id == id).FirstOrDefault();
return View(model);
}
public ActionResult Delete(int id)
{
TblRegister model = dbEntity.TblRegisters.Where(x => x.Id == id).FirstOrDefault();
return View(model);
}
[HttpPost]
public ActionResult Delete(TblRegister model)
{
TblRegister data = dbEntity.TblRegisters.Where(x => x.Id == model.Id).FirstOrDefault();
dbEntity.TblRegisters.Remove(data);
dbEntity.SaveChanges();
return RedirectToAction("index");
}
see my RegisterController↓
index (show list of table data)
create( right click on create method then click add view)
edit
details
delete
Finally Register view folder will be
set your default Controller and Action on RoutConfig.cs on App_Start folder
Build and Run application..
Thank you
For more videos and learn tutorial
https://www.youtube.com/@keytechsoftwares6420
http://keytechsoft.com
Comments
Post a Comment