(i) main Page display code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingMvcapplication1.models;namespacemvcapplication1.controllers{ Public classHomecontroller:controller {// //GET:/home///Home View PublicActionResult Index () {List<student> list =NewSTUDENTDA (). Select (); //viewbag.stu = new Studentda (). Select (); returnView (list); } //Modify Page view public ActionResult Xiugai (string id) {//Viewbag.data = new Studentda (). Select (ID); Student list = new STUDENTDA (). Select (ID); return View (list); }//Update Modify button action public actionresult Update (string sno, String sname, String ssex, DateTime sbirthday, string cl ASS1) {try {new STUDENTDA (). Update (Sno, sname, Ssex, Sbirthday, Class1); Return redirecttoaction ("Index"); } catch (Exception) {return redirecttoaction ("Chucuo", "Home"); } } //delete action public ActionResult Delete (string id) {try {new STUDENTDA ( ). Delete (ID); Return redirecttoaction ("Index"); } catch (Exception) {return redirecttoaction ("Decuowu", "Home"); } } //Add Page view public ActionResult Zengjia () {Student A = new student (); Return View (a); }//Add button action public actionresult Add (student a) {try {new Studen TDA (). Insert (a); Return redirecttoaction ("Index"); } catch (Exception) {return redirecttoaction ("Chucuo", "Home"); } } //Error Page view public ActionResult Chucuo () {return View (); } //Delete error Page view PublicActionResult Decuowu () {returnView (); } //Back to Home button action public ActionResult Tiaozhuan () {return redirecttoaction ("Index"); } }}
(ii) Adding methods to the database
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacemvcapplication1.models{ Public classSTUDENTDA {PrivateMydbdatacontext Context =NewMydbdatacontext (); //Query Student Information public list<student> Select () {return Context.student.ToList (); } //Query Individual student information by school number PublicStudent Select (stringSno) { varquery = Context.student.Where (p=>p.sno==Sno); if(Query. Count () >0) { returnquery. First (); } return NULL; } //Insert Add database public void Insert (Student a) {Context.student.InsertOnSubmit (a); Context.submitchanges (); } //delete public void Delete (string sno) {var query = Context.student.Where (P=>P.SNO==SNO); if (query. Count () >0) {Student Stu = query. First (); Context.student.DeleteOnSubmit (Stu); Context.submitchanges (); } } //Modify public void Update (String sno, String sname, String ssex, DateTime sbirthday, string class1) { var query = Context.student.Where (p = = P.sno = = Sno); if (query. Count () > 0) {Student stu = query. First (); Stu. Sname = Sname; Stu. Ssex = Ssex; Stu. Sbirthday = Sbirthday; Stu. Class = Class1; Context.submitchanges (); } } }}
MVC database additions and deletions (Razor) method (1) and database