Several submission methods for ASP. net mvc forms, asp. netmvc

Source: Internet
Author: User

Several submission methods for ASP. net mvc forms, asp. netmvc

Below is a summary of Several Forms Submission methods in ASP. net mvc.

 

1. Ajax submission form

Need to reference

<Script type = "text/javascript" src = "/Scripts/jquery-1.7.2.min.js"> </script>
<Script src = "/Scripts/jquery. validate. min. js" type = "text/javascript"> </script>
<Script src = "/Scripts/jquery. validate. unobtrusive. min. js" type = "text/javascript"> </script>
<Script src = "/Scripts/jquery. unobtrusive-ajax.min.js" type = "text/javascript"> </script>

The first is the Jquery library, and the second and second are verified. 4th are used for Ajax submission.

Note that when <script src = "/Scripts/jquery. unobtrusive-ajax.min.js" type = "text/javascript"> </script> If you accidentally Reference 2,

What are the consequences? You submitted the task twice when you clicked the submit button. If you don't believe it, try it.

The Code is as follows:

1 @ using (Ajax. beginForm ("AddSubmit", "AdminInfo", new AjaxOptions {HttpMethod = "post "})) 2 {3 <table class = "table"> 4 <tr> 5 <td colspan = "2" class = "TableTitle"> Create an administrator </td> 6 </tr> 7 <tr> 8 <td> @ Html. labelFor (m => m. userName) </td> 9 <td class = "td2"> @ Html. textBoxFor (m => m. userName) </td> 10 </tr> 11 <tr> 12 <td> & nbsp; </td> 13 <td class = "td2"> @ Html. validationMessageFor (m => m. userName) </td> 14 </tr> 15 16 <tr> 17 <td> @ Html. labelFor (m => m. passWord) </td> 18 <td class = "td2"> @ Html. passwordFor (m => m. passWord) </td> 19 </tr> 20 <tr> 21 <td> & nbsp; </td> 22 <td class = "td2"> @ Html. validationMessageFor (m => m. passWord) </td> 23 </tr> 24 25 <tr> 26 <td> @ Html. labelFor (m => m. trueName) </td> 27 <td class = "td2"> @ Html. textBoxFor (m => m. trueName) </td> 28 </tr> 29 <tr> 30 <td> & nbsp; </td> 31 <td class = "td2"> @ Html. validationMessageFor (m => m. trueName) </td> 32 </tr> 33 34 <tr> 35 <td> & nbsp; </td> 36 <td class = "td2"> 37 <input type = "submit" value = "submit"/> </td> 38 </tr> 39 </ table> 40}

If you need to include other parameters in the form, the method is as follows:

@ Using (Ajax. BeginForm ("AddSubmit", "AdminInfo", new {sid = Model. Id}, new AjaxOptions {HttpMethod = "post "}))

 

2. Html direct submission. The Code is as follows:

@ Using (Html. beginForm ("AddSubmit", "AdminInfo", FormMethod. post )) {<table class = "table"> <tr> <td colspan = "2" class = "TableTitle"> Create an administrator </td> </tr> <td> @ Html. labelFor (m => m. userName) </td> <td class = "td2"> @ Html. textBoxFor (m => m. userName) </td> </tr> <td> & nbsp; </td> <td class = "td2"> @ Html. validationMessageFor (m => m. userName) </td> </tr> <td> @ Html. labelFor (m => m. passWord) </td> <td class = "td2"> @ Html. passwordFor (m => m. passWord) </td> </tr> <td> & nbsp; </td> <td class = "td2"> @ Html. validationMessageFor (m => m. passWord) </td> </tr> <td> @ Html. labelFor (m => m. trueName) </td> <td class = "td2"> @ Html. textBoxFor (m => m. trueName) </td> </tr> <td> & nbsp; </td> <td class = "td2"> @ Html. validationMessageFor (m => m. trueName) </td> </tr> <td> & nbsp; </td> <td class = "td2"> <input type = "submit" value = "submit"/> </td> </tr> </table>}

If you need to include other parameters in the form, the method is as follows:

@ Using (Html. BeginForm ("AddSubmit", "AdminInfo", new {sid = 1}, FormMethod. Post ))

 

3. You can write Jquery submission forms by yourself. For details, all Jquery users will write them. Here we will not detail 0-0.

 

This group provides technical support for ASP. net mvc, EF, LINQ, and WEB APIs. It does not care about many people, but about human essence.
ASP. net mvc group 171560784
We invite all experts and beginners to join us.


Several methods for implementing button submission in ASPNET MVC

If webform is used, it does not need to be discussed. However, a form in asp.net mvc can only be submitted to one Action for processing, which is relatively troublesome.

Several essentials for implementing button submission in aspnet mvc [Figure]

Sometimes this problem occurs: Multiple buttons are required on a form to complete different functions, such as a basic approval function. If we use webform, we do not need to discuss it, but a form in asp.net mvc has to be submitted to an Action for processing, which is more troublesome. Example 1: Use a client script such as the reference content in View: <input type = "submit" value = "review suggestion" onclick = 'this. form. action = "<% = Url. action ("Action1") %> "; '/>
<Input type = "submit" value = "not approved" onclick = 'this. form. action = "<% = Url. action ("Action2") %> "; '/>
<Input type = "submit" value = "return" onclick = 'this. form. action = "<% = Url. action ("Action3") %> "; '/> when you click the submit button, modify the action attribute of the Form so that the Form is submitted to the corresponding action of the button for processing. However, at some time, the logic of Action1 and Action2 may be very similar. If you set the value of a field to 1 or 0, it is unnecessary to separate the two actions. Method 2: In the Action, determine which button is submitted to the View. You do not need to use any client script to process the button. Add the name attribute to each button. The reference content is as follows: <input type = "submit" value = "approved" name = "action"/>
<Input type = "submit" value = "not approved" name = "action"/>
<Input type = "submit" value = "return" name = "action"/> then judge in the Controller: [HttpPost]
Public ActionResult Index (string action/* Other parameters */) {if (action = "audit decision") {//} else if (action = "audit decision not approved ") {//} else {//}}

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.