usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Mvc.Ajax;usingMvctable.models;namespacemvctable.controllers{ Public classInternhomecontroller:controller {// //GET:/internhome/Wancemisentities Misdata =Newwancemisentities (); PublicActionResult Index () {returnView (Getallhomes ()); } PrivateIenumerableGetallhomes () {returnMisdata.home.asenumerable(); } // //GET:/INTERNHOME/DETAILS/5 PublicActionResult Details (stringID) {returnView (MISData.Home.First (s = = S.homeid = = id));//Query with the Fist () method } // //GET:/internhome/create PublicActionResult Create () {Home Home=NewHome (); Home. Homeid= Guid.NewGuid (). ToString ();//automatically generate random numbers returnView (home); } // //POST:/internhome/create[Acceptverbs (httpverbs.post)] PublicActionResult Create (home home) {Try { //todo:add Insert Logic here if(Addhomeinfo (Home) >0) returnRedirecttoaction ("Index"); Else returnView (); } Catch { returnView (); } } Private intAddhomeinfo (home home) {Misdata.addobject ("Wancemisentities.home", home);//Add Data returnMisdata.savechanges ();//Submit Data } PublicActionResult Delete (stringId//Delete Data{Misdata.deleteobject (MISData.Home.First (S= = S.homeid = =id)); Misdata.savechanges (); returnRedirecttoaction ("Index");//REDIRECT page } // //GET:/INTERNHOME/EDIT/5 PublicActionResult Edit (stringID) {returnView (MISData.Home.First (s=>s.homeid==id)); } // //POST:/INTERNHOME/EDIT/5[Acceptverbs (httpverbs.post)] PublicActionResult Edit (stringID, home home) { Try { //todo:add update logic hereMISData.Home.First (s = = S.homeid = = ID);//you need to check it first so that there is data in memoryMisdata.applypropertychanges ("Wancemisentities.home", home);//overwrite the data in memoryMisdata.savechanges () >0 returnRedirecttoaction ("Index"); } Catch { returnView (); } } }}
MVC+EF Data Processing