0% found this document useful (0 votes)
13 views

FifthLecture

Uploaded by

mo.amin.abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

FifthLecture

Uploaded by

mo.amin.abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

In The Name of Allah!

CRUD Operation
Insertion

controller code
 public ActionResult Index()
 {
 return View();
 }
 public ActionResult Create(Student obj)
 {
 db.Students.Add(obj);
 db.SaveChanges();
 return RedirectToAction("StudentList");
 }
Insertion
View Code
 <h2>Index</h2>

 <form action="/Test/Create/" method="post">

 <input type="text" name="Name" placeholder="Enter your name" class="form-


control" />
 <input type="text" name="Fname" placeholder="Enter your father name"
class="form-control" />
 <input type="text" name="Department" placeholder="Department Name"
class="form-control" />
 <input type="submit" value="Submit" class="btn btn-primary" />
 </form>
Reading Data
Controller Code
 public ActionResult StudentList()
 {
 var obj = db.Students;
 return View(obj);

 }
Reading
View Code
Editing
Controller Code
 public ActionResult Edit(int id)
 {

 var obj = db.Students.Find(id);


 return View(obj);
 }
 public ActionResult UpdateRecord(Student obj)
 {
 db.Entry(obj).State = EntityState.Modified;
 db.SaveChanges();

 return RedirectToAction("StudentList");
 }
Editing
View Code
 @model Crud.Models.Student
 @{
 ViewBag.Title = "Edit";
 }

 <h2>Edit</h2>

 <form action="/Test/UpdateRecord/" method="post">


 <input type="hidden" name="ID" value="@Model.ID" class="form-control" />
 <input type="text" name="Name" value="@Model.Name" placeholder="Enter your name" class="form-control" />
 <input type="text" name="Fname" value="@Model.Fname" placeholder="Enter your father name" class="form-control" />
 <input type="text" name="Department" value="@Model.Department" placeholder="Department Name" class="form-
control" />
 <input type="submit" value="Update" class="btn btn-primary" />
 </form>
Delete Code

 public ActionResult Delete (int id){


 var obj=db.Students.Find(id);
 db.Students.Remove(obj);
 db.SaveChanges();

 return RedirectToAction("StudentList");
 }

You might also like