Ajax. beginform can be used to submit forms asynchronously.
@ Using (Ajax. beginform ("ajaxformpost", "home", new {id = "11", classname = "firstclass"}, new ajaxoptions {httpmethod = "Post", onbegin = "onbeginpost () ", oncomplete =" onendpost () ", onsuccess =" onsuccesspost ", insertionmode = insertionmode. replace }))
Ajaxformpost is action, home is controller, new {id = "11", classname = "firstclass"} is routing parameter, that is, URL parameter
Ajaxoptions
1. httpmethod: Form submission method.
2. Client JS operations before onbegin form submission.
3. operations that can be returned by the client after the onsuccess form is submitted
4. operations after oncomplete form is submitted
5. insertionmode
// Abstract: // enumerates the Ajax script insertion modes. public Enum insertionmode {// Summary: // Replace the element. replace = 0, // Summary: // Insert before the element. insertbefore = 1, /// Abstract: // Insert after the element. insertafter = 2 ,}
<Div id = "content"> <Table> <tr> <TD> @ HTML. label ("lblname", "name") </TD> <TD> @ HTML. textbox ("txtname") </TD> </tr> <TD> @ HTML. label ("lblage", "Age") </TD> <TD> @ HTML. textbox ("txtage") </TD> </tr> </table> <input type = "Submit" value = "Submit"/> </div>
This is a simple form control, a name, an age, and a submit button.
Next, let's take a look at the corresponding action operation in the home controller. Here we only perform the test, so we only perform the form data.
Public String ajaxformpost (string ID) {string classname = request. querystring ["classname"]; string name = request. form ["txtname"]; string age = request. form ["txtage"]; Return "name" + name + "Age" + age ;}
ID is the parameter of the routing mechanism. Txtname and txtage are obtained through the form. The previous setting is the POST method. Therefore, you must obtain the corresponding value using the request. Form method.
Then return a string. If you want to return this string on the client, you can
<Script Type="Text/JavaScript">FunctionOnsuccesspost (e) {alert (e +"Submitted successfully! ");}</Script>
Of course, if you want to call client JavaScript, you also need to reference a javascript library.
<Script SRC="@URL.Content("~ /Scripts/jquery. unobtrusive-ajax.js")" Type="Text/JavaScript"> </Script>
In this way, the call test can be performed.
Sample Code