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

Lecture 8

The document contains C# code for a CRUD (Create, Read, Update, Delete) operation in an ASP.NET MVC application for managing student records. It includes methods for creating a student with photo upload, listing students, editing student details, and deleting a student. The code also demonstrates how to handle file storage and manage data using Entity Framework.

Uploaded by

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

Lecture 8

The document contains C# code for a CRUD (Create, Read, Update, Delete) operation in an ASP.NET MVC application for managing student records. It includes methods for creating a student with photo upload, listing students, editing student details, and deleting a student. The code also demonstrates how to handle file storage and manage data using Entity Framework.

Uploaded by

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

In The Name of Allah.

Controller code (Insertion)

 public ActionResult Create(Student std,HttpPostedFileBase photo)


 {
 std.photo = storeimage(photo, "/photos/");
 db.Students.Add(std);
 db.SaveChanges();
 return RedirectToAction("ListData");
 }
Image Function

 private string storeimage(HttpPostedFileBase file,string pa)


 {
 if (file != null)
 {
 string filename, extension, Fulname;
 filename = Path.GetFileNameWithoutExtension(file.FileName);
 extension = Path.GetExtension(file.FileName);
 filename = filename + DateTime.Now.Ticks + extension;
 Fulname = Path.Combine(Server.MapPath(pa), filename);
 file.SaveAs(Fulname);
 return pa + filename;
 }
 return "";
 }
Controller code (Getting Data from Data table)

 public ActionResult ListData()


 {
 var Data = db.Students;
 return View(Data);
 }
Controller code (Editing)

 [HttpGet]
 public ActionResult Edit(int id)
 {

 var Data=db.Students.Find(id);
 TempData["rphoto"] = Data.photo;

 return View(Data);
 }
View Code (Editing)

 @model CrudOperation.Models.Teacher

 @{
 ViewBag.Title = "Index";
 }
 <form action="/Teacher/Edit2/" method="post">
 <input type="hidden" name="TID" value="@Model.TID"/>
 <input type="text" name="Name" value="@Model.Name" placeholder="Enter teacher name"
class="form-control" />
 <input type="text" name="FName" value="@Model.FName" placeholder="Enter teacher Fname"
class="form-control" />
 <input type="submit" name="name" value=" UpdateTeacher" class="btn btn-primary" />

 </form>
Controller code (Editing)

 public ActionResult Edit(Student std,HttpPostedFileBase photo)


 {
 if (photo != null)
 {
 std.photo = storeimage(photo, "/photos/");
 }
 else
 std.photo = TempData["rphoto"].ToString();
 db.Entry(std).State = System.Data.Entity.EntityState.Modified;

 db.SaveChanges();
 return RedirectToAction("ListData");
 }
Controller code (Deletion)

 public ActionResult Delete(int id)


 {

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


 db.Students.Remove(Data);
 db.SaveChanges();

 return RedirectToAction("ListData");
 }
THE END

You might also like