Controller operations
The general operation method of the controller is simple to add, delete, query, and modify. The operation object is the students object, and the context is the context connection.
Students objects include name, age, sex information, and Operation pages, which are created using a strongly typed or razor template in mvc3.
1. Define the query Index
Public actionresult index () {var list = context. Students. tolist (); // get the students object information return view (list); // return the list data to the index interface}
2. Define adding a controller
// Get:/student/create
// Display the Add Interface
Public actionresult create () {return view (); // The default view page is Crete. cshtml}
Define Add action
[Httppost] // must follow the submit button to submit the POST request
Public actionresult create (Student) {try {// todo: Add insert logic here if (modelstate. isvalid) {context. students. add (student); // additional object student context. savechanges (); // execute the persistence Operation Return redirecttoaction ("Index"); // return to the index page} catch {// exception information} return view (student );}
3. Define and modify the Controller
// Obtain the page information to be modified. The default page is edit. cshtml.
Public actionresult edit (int id) {VAR model = context. students. find (ID); // obtain the modification information based on the ID query return view (model); // and assign it to the view page}
// Perform the edit operation
[Httppost] public actionresult edit (Student) {// todo: Add update logic here if (modelstate. isvalid) {// automatically identifies the attribute to be modified. entry (student ). state = entitystate. modified; // indicates the modifyed state to be modified. The options include detached, added, unchanged, modifyed, and deleted int I = context. savechanges (); Return redirecttoaction ("Index"); // return to the homepage after successful modification} return view (student );}
4. Define Delete Controller
// Obtain the information to be deleted. The default page is Delete. cshtml public actionresult Delete (int id) {VAR model = context. students. find (ID); Return view (model);} // the operation method [actionname ("delete")] // two identical Delete statements are defined here, therefore, you need to use the actionname feature to specify the delete action [httppost] public actionresult deletepost (int id) // to define it as deletepost. Otherwise, the error {try {// todo: add Delete logic here var student = context. students. find (ID); context. students. remove (student); // The delete operation // Changes to the deleted status context. savechanges (); // persistent return redirecttoaction ("Index") ;}catch {return view () ;}} Delete Using ajax, first modify controller Code : Try {// todo: Add Delete logic here if (request. isajaxrequest () {var student = context. students. find (ID); context. students. remove (student); int K = context. savechanges (); Return content (K. tostring ();} else {return content ("-1 "); // If the returned content is-1, the deletion fails.} catch {return content ("-1 ");}
Modify the deleted link,
Put the original @ html. actionlink ("delete", "delete", new {id = item. studentid })
Change
<A href = "#" name = "delete" SID = @ item. studentid> Delete </a> Use jquery to delete <SCRIPT type = "text/JavaScript" >$ (). ready (function () {$ ("[name = 'delete']"). click (function () {If (confirm ("are you sure you want to delete the information? ") {Var SID = $ (this ). ATTR ("Sid"); var trcontent = $ (this ). parent (). parent (); $. post ("student/delete/", {ID: Sid}, function (data) {If (Data = "-1") {alert ("failed to delete ");} else {$ (trcontent ). remove (); alert ("deleted successfully") ;}}})}) </SCRIPT>
The basic CRUD operations of Asp.net MVC are recorded in the learning process.