Knowledge points used in MVC project development (Ajax. beginform)

Source: Internet
Author: User

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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.