I am totally new to Asp.Net Core and I am trying to implement an Inventory controlling system. Now I am facing a problem of saving sales data to database. The main problem is I have failed to bring data to controller. I have 'sales', 'SalesProducts' and 'products' tables in database. What I have tried so far,
Sales Create View has a dropdown to select products and it populates using SalesViewModel:
SalesViewModel
public class SalesViewModel
{
public Sale Sale { get; set; }
public List<SelectListItem> Products { get; set; }
}
To create sales items list, each time user select a product and it's quantity, 'subArray' will be created and that item array will be pushed to 'SalesItemArray',
$('.btn-sales-add').on('click', function () {
let subArray = [];
let productId = $('#product-id').val();
let productName = $('#product-id option:selected').text();
let price = $('#sales-price').val();
let quanity = $('#sales-quantity').val();
let subTotal = $('#sales-sub-total').val();
quanity = parseInt(quanity);
subTotal = parseFloat(subTotal);
total += subTotal;
$('#sales-total input').val(total);
subArray.push(productId);
subArray.push(productName);
subArray.push(price);
subArray.push(quanity);
subArray.push(subTotal);
salesItemsArray.push(subArray);
});
Array Format
[[pro_id, pro_name, quantity, subTotal],[pro_id, pro_name, quantity, subTotal]]
To bring the sales data and sales items to controller, I used FormData object and another FormDataViewModel as shown in this solution
SalesFormDataViewModel
public class SalesFormDataViewModel
{
public string StoreId { get; set; }
public string Total { get; set; }
public string[] SalesItemList { get; set; }
}
I am passing SalesViewModel to view and passing SalesFormDataViewModel to controller. I am posting the data using Ajax,
let storeId = $("#sales-store-id").val();
let total = $("#sales-total input").val();
let salesItemList = salesItemsArray;var
formData = new FormData();
formData.append("StoreId", storeId);
formData.append("Total", total);
formData.append("salesItemList", salesItemList);
$.ajax({
url: '/api/sales/createSales',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(formData),
success: function (response) {
alert('success');
},
error: function (response, error) {
alert('error');
}
});
The Controller
[HttpPost]
[Route("createSales")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> createSales(SalesFormDataViewModel
salesFormDataViewModel)
{
return Ok(new { success = "Stock updated successfully" });
}
Actually, I have tried almost every solutions searching the web but whatever tried, everytime I get same error response. I can not even reach to the breakpoint in controler because ajax throws an exception. Ajax Response
Please, I am expecting all of your help. Thank you.
I am Editing this question to add more details. After trying Chens solution now getsthis error
"Each time user select a product and it's quantity, 'subArray' will be created and that item array will be pushed to 'SalesItemArray',", could you please share us how are you doring this?SalesFormDataViewModelclass could you please share?