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
)){
Serial number |
Student ID |
Name |
Gender |
Age |
Address |
Operation |
@ Foreach (StuTable info in (ViewData ["data"] as IEnumerable
@ 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: