Mvc3.0 + linq operate on table data in the database (ps: I just learned from cainiao)

Source: Internet
Author: User
1: add the Controller file HomeController. the cs code is as follows: usingSystem; usingSystem. collections. generic; usingSystem. linq; usingSystem. web; usingSystem. web. mvc; usingMvcTestData. models; namespaceMvcTestData. controllers {publicclassHomeContr

1: add the Controller file HomeController. the cs code is as follows: using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using MvcTestData. models; namespace MvcTestData. controllers {public class HomeContr

1: add the Controller file HomeController. cs. The Code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using MvcTestData.Models;namespace MvcTestData.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            TestDataContext txtData = new TestDataContext();            var result=from info in txtData.StuTable                       select info;            ViewData["data"] = result;            return View(result);        }        public ActionResult Add(FormCollection form)        {            string id =form["StuId"];            string name=form["StuName"];            string sex = form["StuSex"];            int age = int.Parse(form["StuAge"]);            string address = form["StuAddress"];            StuTable stu = new StuTable();            stu.StuId = id;            stu.StuName = name;            stu.StuSex = sex;            stu.StuAge = age;            stu.StuAddress = address;            try            {                using (var db = new TestDataContext())                {                    db.StuTable.InsertOnSubmit(stu);                    db.SubmitChanges();                    ViewData["result"] = "ok";                }            }            catch             {                ViewData["result"] = "fail";                throw;            }            return View("Add");        }        public ViewResult AddInfo()        {            return View("AddInfo");        }        public ViewResult Delete()        {            int id = Int16.Parse(Request.Form["id"]);            try            {                using (var db = new TestDataContext())                {                    db.StuTable.DeleteOnSubmit(db.StuTable.First(info => info.ID == id));                    db.SubmitChanges();                    ViewData["result"] = "ok";                }            }            catch            {                ViewData["result"] = "fail";                throw;            }            return View("Delete");        }           }}

View Code

2: Add a linq to SQL class file to the models folder and copy the tables in the database.


3: Add views for actions in the Controller

4 View Index. cshtml code

@ Using MvcTestData. Models    Index    

)){ @ Foreach (StuTable info in (ViewData ["data"] as IEnumerable }
Serial number Student ID Name Gender Age Address Operation
@ Info. ID @ Info. StuId @ Info. StuName @ Info. StuSex @ Info. StuAge @ Info. StuAddress

@ Html. ActionLink ("add personal information", "AddInfo", "Home ")

View Code

5 view Add. cshtml code

@ Model MvcTestData. Models. StuTable @ {ViewBag. Title = "Add" ;}@ if (ViewData ["result"]. Equals ("OK ")){

Added

} Else {

Failed to add

}

View Code

6 view AddInfo. cshtml code

@ Model MvcTestData. Models. StuTable @ {ViewBag. Title = "AddInfo";} AddInfo@ Using (Html. BeginForm ("Add", "Home", FormMethod. Post )){

Student ID: @ Html. TextBoxFor (x => x. StuId)

Student name: @ Html. TextBoxFor (x => x. StuName)

Student gender: @ Html. TextBoxFor (x => x. StuSex)

Student age: @ Html. TextBoxFor (x => x. StuAge)

Student address: @ Html. TextBoxFor (x => x. StuAddress)

}

View Code

7 view Delete. cshtml code

@ Model MvcTestData. Models. StuTable @ {ViewBag. Title = "Delete" ;}@ if (ViewData ["result"]. Equals ("OK ")){

Deleted

} Else {

Deletion failed

}

View Code

8. Final Test Result diagram:

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.