1

I become engage, I have a controller and in view I try to get data as json via jquery ajax. I get data as List , string , ... but as a model? no never. I used this type of code several times, but today it doesn't work. My God. Controller

[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);
    }

I used breakpoint and detect model has values, but I get error json.

My View

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);
        }
    });
}
2
  • 1
    Possibly have a look at this stackoverflow.com/questions/4564341/… Commented Jul 13, 2018 at 16:08
  • of course I used IActionResult instead of Jsonresult, but it doesn't work again. Commented Jul 13, 2018 at 16:10

2 Answers 2

0

Try changing your return from

return Json(model)

to

return Json(model, JsonRequestBehavior.AllowGet)
Sign up to request clarification or add additional context in comments.

1 Comment

awfully I've tested it before. result is same, error
0

as mentioned in comment by @JamesS, https://github.com/Tratcher/EDES/blob/a5e783cf4d1689590a07f10c1a48ffc7f0981352/EDES/Controllers/ErrorController.cs#L43

this works,

[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();
        }

Comments

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.