c# - Post value from javascript to Controller -
in our mvc project using following javascript function , have ensured working function without errors.
function getselecteditems() { var newitemids = []; $("#selectitems tbody").find('tr').each( function () { var id = $(this).find('#hfid').val(); var isadd = $(this).hasclass('k-state-selected'); if (isadd == true) { newitemids.push(id); } }); jquery.ajaxsettings.traditional = true $.post("/form8invoice/additemtocart", { newitemids: newitemids });
however when posting array values controller, seems values not getting passed. controller action method given below quick reference.
[httppost] public actionresult additemtocart(string[] newitemids, [datasourcerequest] datasourcerequest request) { if (newitemids != null) { float grandtotal = 0; string currentlocation = getcurrentlocation(); foreach (string itemid in newitemids) { location location = new location(); stock stock = new stock(); float unitprice = 0; float sellingprice = 0; if(string.isnullorempty(currentlocation)) { list<stock> stoklist = _stockrepositoty.getstocksbyitemid(new guid(itemid)).tolist(); stock = stoklist[0]; } else if (currentlocation != "admin") { location = _locationrepository.getlocationbyname(currentlocation); stock = _stockrepositoty.getstockbyitemidandlocation(new guid(itemid), location.locationid); } else { list<stock> stoklist = _stockrepositoty.getstocksbyitemid(new guid(itemid)).tolist(); stock = stoklist[0]; } if (stock != null) { if (!string.isnullorempty(stock.unitprice.tostring())) { unitprice = float.parse(stock.unitprice.tostring()); sellingprice = unitprice; // + tax - discount } } if (_cartitemsrepository.isitemalreadyadded(getcurrentlocation(), getcurrentuser(), new guid(itemid))) { cartitem cartitem = _cartitemsrepository.getitem(getcurrentlocation(), getcurrentuser(), new guid(itemid)); int quantity = cartitem.quantity; cartitem.quantity = cartitem.quantity + 1; _cartitemsrepository.updateitems(cartitem); float netamount = sellingprice * cartitem.quantity; grandtotal = grandtotal + netamount; } else { cartitem cartitem = new cartitem(); cartitem.locationname = getcurrentlocation(); cartitem.username = getcurrentuser(); cartitem.itemid = new guid(itemid); cartitem.quantity = 1; _cartitemsrepository.insertitems(cartitem); float netamount = sellingprice * cartitem.quantity; grandtotal = grandtotal + netamount; } } viewbag.grandtotal = grandtotal.tostring(); _cartitemsrepository.save(); } return view(); }
any appreciated.
it looks you've got capitalization of parameter property wrong. try this:
$.post("/form8invoice/additemtocart", { newitemids: newitemids });
Comments
Post a Comment