0

In our MVC project we are using the following javascript function and we have ensured that this is a working function without any 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 the array values to Controller, it seems that the values are not getting properly passed. The controller action method is given below for 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 help will be appreciated.

3
  • I'm getting the issue only after hosted the website in IIS. In development environment everything is working properly. Commented Mar 21, 2014 at 11:39
  • change your controller parameter to match the Json one. It needs to be string[] NewItemIDs Commented Mar 21, 2014 at 13:10
  • Did you try $.post("/Form8Invoice/AddItemToCart", JSON.stringify(newItemIDs)); Commented Mar 21, 2014 at 21:57

1 Answer 1

1

It looks like you've got the capitalization of the parameter property wrong. Try this:

$.post("/Form8Invoice/AddItemToCart", { newItemIDs: newItemIDs });
Sign up to request clarification or add additional context in comments.

2 Comments

In Asp.Net MVC matches are case insensitive and do not depend on the parameter order.
I'm getting the issue only after hosted the website in IIS. In development environment everything is working properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.