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.
string[] NewItemIDs