Friday, March 4, 2011

Using the jQuery validatation plugin to send multiple values to an ASP.NET MVC controller action?

Hi,

Using the jQuery Validation plugin and AJAX, how can I validate the contents of say an input (textbox) but pass more than one parameter to a controller action?

A brilliant example of passing a single value via AJAX using the plugin can be found here.

From stackoverflow
  • Something like this?

    $(document).ready(function(){
      $("#form-sign-up").validate( {
        rules: {
          email: {
            required: true,
            email: true
          },
          surname: {
            required: true,
            surname: true
          }
        },
        messages: {
          email: {
            required: "Please provide an email",
            email: "Please provide a valid email"
          },
          surname: {
            required: "Please provide a surname",
            surname: "Please provide a valid surname"
          }
        }
      });
    });
    

    edit found a large demo here

    Kieron : The problem I have is that based on the user-name and the value of a hidden field goes into making a unique pair. So when validating the login name I need to pass both the login name and the hidden field value to an action. If you check the example in the question you'll see what I mean.
  • Looking at the code for jQuery Validation it looks like the post data can not be customized. So you'll have to stick with query parameters:

     <script type="text/javascript">
    $(document).ready(function(){
      $("#form-sign-up").validate(
      {
        var param1 = $('#mytextbox').val();
    
        rules:
        {
            login:
            {
              required: true,
              remote: '<%=Url.Action("IsLoginAvailable", "Accounts") %>?param1=' + param1
            }
          }  
        });
    
    });
    </script>
    
  • The correct Script will be

    remote: function() { var p = $j('#productName').val(); return "../Product/LookupRevision?p=" + p; } instead of

    remote: "../Product/LookupRevision"

0 comments:

Post a Comment