ASP.NET MVC Shopping Cart & jQuery -


i have pricing page display list of products along prices. user can choose add multiple products shopping cart (active shopping cart being shown on right hand side of page). how have implemented using ajax/jquery...

code snippet view (aspx): looping thro available products in viewmodel , displaying details:

<% foreach (var _product in _supplier.hotelproducts)     { %>     <tr>         <td colspan="2" align="left" valign="top"><% = _product.description %></td>         <td  align="left">             <% using (html.beginform("addtocart", "shoppingcart", formmethod.post, new { @class = "addproducttocartform" }))                 { %>                 <input type="hidden" name="hsupplierid" id="hsupplierid" value="<% = _supplier.id %>" />                 <input type="hidden" name="hproductcode" id="hproductcode" value="<% = _product.code %>" />                 <input type="hidden" name="hproductdescription" id="hproductdescription" value="<% = _product.description %>" />                 <input type="hidden" name="hproductprice" id="hproductprice" value="<% = _product.totalprice %>" />                 <input type="submit" value="+ add cart" />             <% } %>         </td>         <td valign="top" align="center">             <span id="spanproductprice" class="_price">$<% = _product.totalprice %></span>         </td>     </tr> <% } %>     

as can see above code snippet, have "+ add cart" button againts each product , requirement pass supplierid , product details (code, desc & price) controller , cart. please note list of products & pricing external webservice , there no way me pass product code , retrieve corresponding description & price on server side, that's why need capture required product details when user adds cart.

$(function () {     $(".addproducttocartform").submit(function (e) {         e.preventdefault();         var hiddencartform = {             supplierid: $(this.hsupplierid).val(),             code: $(this.hproductcode).val(),             description: $(this.hproductdescription).val(),             totalprice: $(this.hproductprice).val()         };         $.post($(this).attr("action"), hiddencartform, function (data) {             //alert("success");             rendercart(data);         });         return false; // form submitted using ajax, don't submit again regular way      }); });  function rendercart(data) {     $("#rightcolumn").html(data); }  

here custom hiddencartform object use pass information view controller via jquery

public class hiddencartform     {         public string supplierid { get; set; }         public string code { get; set; }         public string description { get; set; }         public decimal? totalprice { get; set; }         //public productview product { get; set; }     } 

i have 2 questions:

[1] there better way handle scenario? little uncomfortable many forms & hidden fields (for holding supplierid & product details) on view. these forms & hidden fields visible when view source.

[2] need pretty of information "_product" when user adds particular product shopping cart. there better way pass information via jquery instead of using hidden fields looping thro products foreach (var _product in _supplier.hotelproducts) in view?

i on mvc 2 currently.

why need pass product's description , price in form post? couldn't controller action handles ajax post values database? depending on how model defined, supplier id might removed similar reasons.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -