1

我开始参与,我有一个控制器,并且我尝试通过 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);
        }
    });
}
4

2 回答 2

0

尝试更改您的退货

return Json(model)

return Json(model, JsonRequestBehavior.AllowGet)
于 2018-07-13T16:10:31.803 回答
0

正如@JamesS 评论中提到的,https://github.com/Tratcher/EDES/blob/a5e783cf4d1689590a07f10c1a48ffc7f0981352/EDES/Controllers/ErrorController.cs#L43

这有效,

[HttpPost]
        [Authorize(AuthenticationSchemes = ApiKeyAuthDefaults.AuthenticationScheme)]
        public IActionResult Post([FromBody]JObject body)
        {
            if (body == null)
            {
                return BadRequest();
            }
            
            var report = new ErrorReport()
            {
                Created = DateTimeOffset.UtcNow,
                Message = body.GetValue("message").Value<string>(),
                Version = body.GetValue("version").Value<string>(),
                Json = body.GetValue("json").ToString(Formatting.None),
            };

            _context.ErrorReports.Add(report);
            _context.SaveChanges();

            return Ok();
        }
于 2020-09-18T07:58:32.197 回答