Hi friends, in this article I will explain about using ASP.NET MVC4 application using in the Razor view engine and Entity Framework to Create / Insert, Delete,Update,Search and Read functionality || MVC4 Razor CRUD Operation using C#.NET.
I already explained in the previous articles about MVC 4 Razor:How to Create Simple Login Page/Form in ASP.NET using MVC 4, MVC4 Razor: Show Multiple Models in a Single View using dynamically created object. and Enhancing WebGrid with Insert Update and Delete Operations Using Repository Pattern with Entity Framework
Create
one STUDENTS table as below.
CREATE TABLE STUDENTS(
ID INT PRIMARY KEY IDENTITY,
FIRSTNAME
VARCHAR(50),
LASTNAME VARCHAR(50),
ADDRESS VARCHAR(150)
)
|
Right click on root Tag add new Item –click
left side Data --ADO.NET Entity Data
model and give name as
StudentModel.edmx as shown in the below figure.
Add your connection and give the Entity name (web.config connectionstring) as StudentEntity and Model as STUDENTModel
Add your connection and give the Entity name (web.config connectionstring) as StudentEntity and Model as STUDENTModel
Press Cntl+Shift+B or Build the
solution.
StudentController.cs
using System;
using
System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcStudentData.Controllers
{
public
class StudentController
: Controller
{
private StudentEntities
db = new StudentEntities();
//
// GET: /Student/
public ActionResult
Index()
{
return View(db.STUDENTS.ToList());
}
//
// GET: /Student/Details/5
public ActionResult
Details(int id = 0)
{
STUDENT student = db.STUDENTS.Single(s =>
s.ID == id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
//
// GET: /Student/Create
public ActionResult
Create()
{
return View();
}
//
// POST: /Student/Create
[HttpPost]
public ActionResult
Create(STUDENT student)
{
if (ModelState.IsValid)
{
db.STUDENTS.AddObject(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
//
// GET: /Student/Edit/5
public ActionResult
Edit(int id = 0)
{
STUDENT student = db.STUDENTS.Single(s =>
s.ID == id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
//
// POST: /Student/Edit/5
[HttpPost]
public ActionResult
Edit(STUDENT student)
{
if (ModelState.IsValid)
{
db.STUDENTS.Attach(student);
db.ObjectStateManager.ChangeObjectState(student, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
//
// GET: /Student/Delete/5
public ActionResult
Delete(int id = 0)
{
STUDENT student = db.STUDENTS.Single(s =>
s.ID == id);
if (student == null)
{
return HttpNotFound();
}
return
View(student);
}
//
// POST: /Student/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult
DeleteConfirmed(int id)
{
STUDENT student = db.STUDENTS.Single(s =>
s.ID == id);
db.STUDENTS.DeleteObject(student);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool
disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
|
Run the application then the output will like as shown in the below figures.
nice article
ReplyDeleteThank You Ravi Shanker
Deletethank you very nice post
ReplyDeleteWebsite Development in india
using screenshots providing a well explanation which mvc informative are most helpful to know more. It explaining with coding representation successfully.
ReplyDeleteDot Net Training in Chennai
I would be really understand for this concepts of MVC it's most helpful to me.I'll working this code result was really successful..
ReplyDeleteDotnet Training in Chennai
Very useful & Informative
ReplyDeleteBest Android Training in Chennai, Velachery
Best ios Training in Chennai, Velachery
Best PHP Training in Chennai, Velachery
Best Dot Net Training in Chennai, Velachery
Thank you for taking the time to provide us with your valuable information.
ReplyDeleteFreshers Jobs in Chennai
Thank you for taking the time to provide us with your valuable information.
ReplyDeleteFreshers Jobs in Chennai
This comment has been removed by the author.
ReplyDeleteIt is very good and useful for students and developer .Learned a lot of new things from your post!Good creation ,thanks for good info .Net Online Training Bangalore
ReplyDelete