Project is also used in the usual three-tier architecture, and then the web side is MVC. All right, that's easy.
First, display the data list
Take the data, turn the DataTable into a list class, and put it in the viewbag.
PublicActionResult Index () {List<Package.Model.Message> lists =NewList<package.model.message>(); Messagedal message=NewMessagedal (); DataSet DS= message. GetList ("1=1"); for(inti =0; I < DS. tables[0]. Rows.Count; i++) {lists. ADD (NewPackage.Model.Message () {gname= ds. tables[0]. rows[i]["Gname"]. ToString (), Gcontent= ds. tables[0]. rows[i]["gcontent"]. ToString ()}); } //returns the data that needs to be boundViewbag.guestbooklist =lists; returnView (); }
The display on the index.cshtml page
< Div > @foreach (Package.Model.Message item in viewbag.guestbooklist) { <span >:</span> @item. gcontent.tostring () < /> } </div>
Second, add data
1. First make a form on the index.cshtml page, prompt the action address, point to the Controller a class name
<formID= "Formguestbook"Method= "POST"Action= "/index/add"><!--class name/method name -Name:<inputtype= "text"ID= "Txtname"name= "Txtname" /><BR/>content:<inputtype= "text"ID= "Txtcontent"name= "Txtcontent" /><BR/> <inputtype= "Submit"name= "Btnsubmitguestbook"value= "Submit Message" /> </form>
2. Finally, after the request in the Controller page, add to the database, and then use the JS statement to return to the submission page
PublicActionResult Add () {//Get form submission Data stringGname = request.form["txtname"]; stringGcontent = request.form["txtcontent"]; //Insert DatabaseMessagedal message =NewMessagedal (); Message. ADD (NewPackage.Model.Message () {gname=Gname, Gcontent=gcontent}); //Back to List pageResponse.Write ("<script>alert (' message success! '); location.href= '/index ' </script>"); return NULL; }
ASP. NET mvc-simple additions and deletions check and change