title: Using $("form").serialize() in E-Commerce Shopping Cart
body: <p>I am working on an e-commerce application shopping cart. I have a pricing page which display a list of products and their prices for multiple categories. User can choose to add multiple products to shopping cart (active shopping cart is being shown on right hand side of page). I am trying to use Ajax/jQuery for adding items to my cart. I have a form wrapped around each product which contains multiple hidden fields I would like to pass to my function and to the controller. You can see all these in the code below:</p>
  
  <pre><code>&lt;% foreach (var _category in Model) { %&gt;
      &lt;% foreach (var _product in _category.Products)
          { %&gt;
          &lt;tr&gt;
              &lt;td align="left" valign="top"&gt;&lt;% = _product.Description %&gt; (&lt;% = _product.Code %&gt;)&lt;/td&gt;
              &lt;td valign="top" align="center"&gt;$&lt;% = _product.TotalPrice %&gt;&lt;/td&gt;
              &lt;td align="left"&gt;
                  &lt;form id="frmProduct_&lt;%=_product.Code%&gt;"&gt;
                      &lt;input type="button" onclick="JavaScript:addProductToBasket(this.form);" value="+ Add to cart" /&gt;
                      &lt;input type="text" id="hProductCode" value="&lt;% = _product.Code %&gt;" /&gt;
                      &lt;input type="text" id="Text1" value="&lt;% = _product.TotalPrice %&gt;" /&gt;
                      &lt;!--Other hidden fields for passing data --&gt;
                  &lt;/form&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;% } %&gt;    
  &lt;% } %&gt;    
  </code></pre>
  
  <p>Since I have multiple forms on page, I am having hard time accessing a particular form inside my javascript function. What is a best way to handle this scenario?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function addProductToBasket(_form) {
          alert('Hi');
          var str = $('#_form').serialize();
          alert(str);
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>I am using ASP.NET MVC 2.0 and cannot move to MVC 3.0 at the moment.</p>