MVC + jQuery development B/S system: Form submission (1)

Source: Internet
Author: User

JqueryIs an excellent protocol Cr protocol pt framework.MVCIs a design pattern that forcibly separates the input, processing, and output of an application. Today we will talk about how to use JQuery + MVC to process form submission.

Expected results:

1. Form Verification before submission

2. Form Verification

3. Submitted feedback

OK. Let's talk about how it works.

The front-end <form action = 'xxx. aspx 'method = 'post'> </form> specifies the processing page for accepting forms. Method Here we only talk about post. If you submit a form using ajax, xxx. aspx is the request url. The callback after the ajax request is the submission result of the Response Form (error, success). The feedback we submit is represented by a float layer (lightbox. Instead of using alert (""); in this way, too many bytes are very busy.

We introduced a Jquery plug-in http://www.malsup.com/jquery/form/#api which is a slightly famous plug-in for ease of use. You can search for its usage.

What we need to do is:

1. Use of jquery. form. js

2. Implementation of lightbox

3. How to verify

Code:

 
 
  1. $. Fn. submitForm = function (args)
  2. {
  3. Var url, id, callback, before;
  4. Id = this. attr ("id ");
  5. If (typeof (args) = "string ")
  6. {
  7. Url = args;
  8. Before = undefined;
  9. Callback = undefined;
  10. }
  11. Else
  12. {
  13. Args = args | new Object ();
  14. Url = args. url | this. attr ("action ");
  15. If (typeof (args) = "function ")
  16. {
  17. Callback = args;
  18. }
  19. Else
  20. {
  21. Before = args. before;
  22. Callback = args. callback;
  23. }
  24. }
  25. // Input verification
  26. This. inputValidate ();
  27. // If the form does not have a url, It is a pseudo-commit.
  28. If (url = undefined | url = "")
  29. {
  30. $ ("#" + Id). submit (function (){
  31. If ($ ("#" + id). submitValidate () = false)
  32. Return false;
  33. // Call back if the verification is successful.
  34. Callback ();
  35. });
  36. }
  37. Else
  38. {
  39. This. ajaxForm ({
  40. Url: url,
  41. BeforeSubmit: function (a, f, o)
  42. {
  43. // Submit for verification
  44. If ($ ("#" + id). submitValidate () = false)
  45. Return false;
  46. If (before! = Undefined & before () = false ){
  47. Return false;
  48. }
  49. O. dataType = "json ";
  50. },
  51. Success: function (data ){
  52. If (data = "" | data = null)
  53. {
  54. Return false;
  55. }
  56. $ ("# MyMsg"). showMsg (data, callback );
  57. Return false;
  58. }
  59. });
  60. }
  61. Return this;
  62. };

The above method is very simple, that is, the use of the form plug-in, there are several methods we need to implement. (InputValidate, submitValiedate, ajaxMsg)
Since ajax is used for submission, we need to give the client a feedback and then present it with Lightbox.


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.