Simple MVC instance and mvc instance
Using System; using System. collections. generic; using System. linq; using System. text. regularExpressions; using System. web; using System. web. mvc; using WebApplication1.Models; namespace WebApplication1.Controllers {public class LeaveMsgController: Controller {// GET:/LeaveMsg/public ActionResult Index () {// Controllers: save the data first and then obtain string s = "hello!" in the view !! "; ViewData [" aa "] = s; BBSDataContext bbs = new BBSDataContext (); List <student> v = (from m in bbs. student select m ). toList (); ViewData ["list"] = v; return View () ;}// Delete public ActionResult Delete () {// RouterData can obtain the data string id = RouteData on the route. values ["id"]. toString (); // you can return a piece of text to the browser. // return view () returns the view corresponding to the current Action to the browser BBSDataContext dc = new BBSDataContext (); var v = from d in dc. student w Here d. stuID = int. parse (id) select d; if (v. count ()> 0) {// Delete try {dc. student. deleteOnSubmit (v. first (); dc. submitChanges (); // success // temporary TempData, serving the next request. The next request is complete, data clearing // in essence, the session stores data TempData ["msg"] = "deleted successfully";} catch (Exception) {// failed TempData ["msg"] = "failed to delete !!! ";}} Else {// no corresponding data. TempData [" msg "] =" no corresponding data found !!! ";}Return RedirectToAction (" Index ") ;}// added public ActionResult Insert () {return View () ;}// public void CheckData (student s) for data verification) {// user name cannot be blank if (s. stuName = "") {ModelState. addModelError ("stuName", "username cannot be blank");} // gender content length cannot be blank and cannot exceed 2 characters if (s. stuNo = "" & s. stuNo. length <2) {ModelState. addModelError ("stuName", "the user name cannot be blank or the number of characters is greater than 2");} // Regular Expression // bool B = Regex. isMatch (s. stuID. toString (), "^ \ {6} $"); // C # positive Then} // submit the new Operation public ActionResult InsertOK () {// data verification BBSDataContext dc = new BBSDataContext (); student s = new student (); s. stuName = Request ["stuName"]; s. stuID = int. parse (Request ["stuNo"]); s. sex = Request ["sex"]; s. remark = Request ["remark"]; CheckData (s); if (! ModelState. isValid) // The verification fails. {ViewData ["stuName"] = s. stuName; ViewData ["stuNo"] = s. stuNo; ViewData ["sex"] = s. sex; return View ("Insert");} try {dc. student. insertOnSubmit (s); dc. submitChanges (); TempData ["msg"] = "added successfully !!! "; Return RedirectToAction (" Index ");} catch (Exception) {TempData [" msg "] =" failed to add "; return RedirectToAction (" Insert ");}} // Edit jump public ActionResult Edit () {string id = RouteData. values ["id"]. toString (); ViewData ["id"] = id; // find the data corresponding to the id BBSDataContext dc = new BBSDataContext (); var v = from d in dc. student where d. stuID = int. parse (id) select d; if (v. count ()> 0) {var d = v. first (); ViewData ["st UName "] = d. stuName; ViewData ["stuNo"] = d. stuNo; ViewData ["sex"] = d. sex; ViewData ["remark"] = d. remark; return View () ;}else {TempData ["msg"] = "no corresponding data"; return View () ;}// modify public ActionResult Update () {string id = Request ["id"]; BBSDataContext dc = new BBSDataContext (); var v = from d in dc. student where d. stuID = int. parse (id) select d; if (v. count ()> 0) {var m = v. first (); m. stuName = Request ["stuName"]; m. stuNo = Request ["stuNo"]; m. sex = Request ["sex"]; CheckData (m); if (! ModelState. isValid) {return View ("Edit");} try {dc. submitChanges (); // submit and modify TempData ["msg"] = "modified successfully"; return RedirectToAction ("Index");} catch (Exception) {TempData ["msg"] = "modification failed! "; Return RedirectToAction (" Edit ", new {id = id });}} else {// no such data TempData ["msg"] = "data has been deleted by another user before the data is submitted !! "; Return RedirectToAction (" Index ");}}}}Business Logic in controller @ {ViewBag. title = "Index ";} @ using WebApplication1.Models