我开始参与,我有一个控制器,并且我尝试通过 jquery ajax 将数据作为 json 获取。我将数据作为 List , string ,...但作为模型?没有永不。我多次使用这种类型的代码,但今天它不起作用。我的上帝。 控制器
[HttpPost]
public JsonResult UpdatedShoppingCartRentalItems()
{
var subTotalIncludingTax = _workContext.TaxDisplayType == TaxDisplayType.IncludingTax && !_taxSettings.ForceTaxExclusionFromOrderSubtotal;
var shoppingCartItems = _workContext.CurrentCustomer.ShoppingCartItems;
var model = new List<RentalMiniCart>();
if (shoppingCartItems.Any())
{
foreach (var item in shoppingCartItems)
{
var product = _productService.GetProductById(item.ProductId);
var row = new RentalMiniCart()
{
ShoppingCartItem = item,
ProductSeName = product.GetSeName()
};
if (item.RentalStartDateUtc != null && item.RentalEndDateUtc != null)
{
// rental product
// number of days
var numberofDays = 1;
if (item.RentalStartDateUtc != item.RentalEndDateUtc)
{
//endDate = endDate.AddDays(-1);
var numberOfDaysTimeSpan = item.RentalEndDateUtc - item.RentalStartDateUtc;
numberofDays = numberOfDaysTimeSpan.Value.Days;
}
var previousDecimalPrice = numberofDays * product.Price;
row.PreviousPrice = _priceFormatter.FormatPrice(previousDecimalPrice, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
var currentDecimalPrice = RentalSystemHelper.CalculateRentalPrice(product.Id, item.RentalStartDateUtc, item.RentalEndDateUtc);
row.CurrentPrice = _priceFormatter.FormatPrice(currentDecimalPrice, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
}
else
{
row.PreviousPrice = _priceFormatter.FormatPrice(product.Price, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
row.CurrentPrice = _priceFormatter.FormatPrice(product.Price, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
}
model.Add(row);
}
}
return Json(model);
}
我使用断点并检测模型有值,但我得到错误 json。
我的观点
function CorrectMiniCartItems() {
$.ajax({
type: 'POST',
url: "@Html.Raw(Url.Action("UpdatedShoppingCartRentalItems", "MiscNopshopRentalSystem"))",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
/*data: data,*/
success: function (result) {
console.log("success result: " + result);
// Code goes here
},
error: function(result) {
console.log("error result: "+result);
}
,
complete : function (result) {
//console.log("complete result: " + result);
}
});
}