MVC database additions and deletions (Razor) method (1) and database

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.