Я делаю ajax вызов контроллера asp.net mvc, который публикует данные и возвращает json.
Это работает правильно, однако я получаю следующие ответы.Они не всегда одинаковы, но это дает представление о том, что происходит.
Контроллер
[Authorize]
[HttpPost]
public JsonResult Index([DefaultValue(0)]int id, UserSession userSession)
{
try
{
var productId = int.Parse(Request.QueryString["productId"]);
var company = _companyRepository.Get(userSession.CompanyId);
var previousProductTotal = decimal.Parse(Request.Form[string.Format("collectivetotalprice-{0}", productId)]);
var auditOutput = new StringBuilder();
var campaign = _campaignRepository.Get(id);
campaign.SubTotal = (campaign.SubTotal - previousProductTotal);
var product = _productRepository.Get(productId);
var productCampaigns =
_productCampaignRepository.FindByCompanyIdAndCampaignIdAndProductId(company.Id,
campaign.Id, productId);
var totalQuantity = 0;
foreach (var campaignRetailer in campaign.CampaignRetailers)
{
var retailerName = campaignRetailer.Retailer.Name.Replace(" ", "").ToLower();
var inputRetailerOrder = string.Format("input-retailerorder-{0}", campaignRetailer.Retailer.Id);
var orderId = int.Parse(Request.Form[inputRetailerOrder]);
var order = _orderRepository.Get(orderId);
foreach (var productCampaign in productCampaigns)
{
var inputName = string.Format("input-{0}-{1}", retailerName, productCampaign.Product.Id);
var input = Request.Form[inputName];
var quantity = StringHelper.IntParse(input);
if (quantity > 0) totalQuantity += quantity;
int previousQuantity = 0;
var orderItem = _orderItemRepository.GetByCompanyIdAndCampaignIdAndRetailerId(company.Id, campaign.Id, campaignRetailer.Retailer.Id, productCampaign.Product.Id);
if (orderItem == null)
{
orderItem = OrderHelper.CreateCampaignOrderItem(order, productCampaign.Product, company,
campaign, campaignRetailer.Retailer, quantity);
order.AddOrderItem(orderItem);
_orderRepository.Update(order);
}
else
{
previousQuantity = orderItem.Quantity;
orderItem.Quantity = quantity;
_orderItemRepository.Update(orderItem);
}
AuditHelper.UpdatedOrderItems(orderItem.Quantity, previousQuantity, orderItem.Product.Name,
retailerName, auditOutput);
}
}
var priceBreak = PriceBreakHelper.Choose(totalQuantity, product);
var response = new CampaignOrderResponse();
var productTotal = priceBreak.Price*totalQuantity;
campaign.SubTotal += productTotal;
_campaignRepository.Update(campaign);
var auditMessage = (!string.IsNullOrEmpty(auditOutput.ToString()))
? string.Format("<table class='pc-100'>{0}</table>", auditOutput.ToString())
: auditOutput.ToString();
var userProfile = _userProfileRepository.Get(userSession.UserName);
UpdateAudit(company, campaign, userProfile, auditMessage);
response.CollectiveQuantity = totalQuantity.ToString();
response.CollectivePrice = StringHelper.Currency(priceBreak.Price);
response.CollectivePriceFormatted = priceBreak.Price.ToString();
response.CollectiveTotalPrice = productTotal.ToString();
response.CollectiveTotalPriceFormatted = StringHelper.Currency(productTotal);
response.Production = StringHelper.Currency(campaign.SubTotal);
response.TotalProduction = StringHelper.Currency(campaign.TotalProduction);
response.Delivery = StringHelper.Currency(campaign.Delivery);
response.Variant = StringHelper.Currency(campaign.Variant);
response.Status = "Updated";
return this.Json(response, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
var response = new CampaignOrderResponse();
response.Status = "UpdateProduct: " + ex.Message;
return this.Json(response, JsonRequestBehavior.AllowGet); ;
}
}
</p>
<p>$.ajax({
type: "POST",
url: quickResponseUrl,
data: $("form").serialize(),
success: function (data) {
if (data.Status == "Updated") {
$(currentButton).html(complete);
var collectquantity = priceElement + " #collectivequantity-" + productId;
var collectiveprice = priceElement + " #collectiveprice-" + productId;
var collectivepriceformatted = priceElement + " #collectivepriceformatted-" + productId;
var collectivetotalpriceformatted = priceElement + " #collectivetotalpriceformatted-" + productId;
var collectivetotalprice = priceElement + " #collectivetotalprice-" + productId;
//alert(collectivetotalprice);
$(collectquantity).html(data.CollectiveQuantity);
$(collectiveprice).html(data.CollectivePrice);
$(collectivepriceformatted).html(data.CollectivePriceFormatted);
$(collectivetotalpriceformatted).html(data.CollectiveTotalPriceFormatted);
$(collectivetotalprice).val(data.CollectiveTotalPrice);</p>
<code> $("#campaign-totalproduction").html(data.TotalProduction);
$("#campaign-delivery").html(data.Delivery);
$("#campaign-variant").html(data.Variant);
$("#campaign-production").html(data.Production);
UpdateRetailers();
console.log(new Date() + ": First Request :" + data.Status);
}
else {
$(".wrapper-matrixStatus").html("<span class='error campaign-status'>Error with first submission " + data.Status + "</span>").show();
$(currentButton).html(complete);
}
},
error: function (data) {
$(".wrapper-matrixStatus").html("<span class='error campaign-status'>Error with first submission " + data.Status + "</span>").show();
$(currentButton).html(complete);
}
});
</code>