ASP.NET CORE AJAX 请求

huangapple go评论111阅读模式
英文:

ASP.NET CORE AJAX request

问题

以下是您要翻译的内容:

"I want to submit my form data, but before I wanted to check my query and get the data in the
objOrderViewModel
I checked with debugger, no data comes, only DateTime, what could be the problem?

Controller

  1. [HttpPost]
  2. public JsonResult Index(OrderViewModel objOrderViewModel)
  3. {
  4. return Json(data: "");
  5. }

My js

  1. function FinalPayment() {
  2. var objOrderViewModel = {};
  3. var ListOrderDetailViewModel = new Array();
  4. objOrderViewModel.PaymentTypeId = $("#PaymentType").val();
  5. objOrderViewModel.CustomerId = $("#Customer").val();
  6. objOrderViewModel.FinalTotal = $("#txtFinalTotal").val();
  7. $("#tblRestaurantItemList").find("tr:gt(0)").each(function () {
  8. var objOrderDetailViewModel = {};
  9. objOrderDetailViewModel.ItemId = $(this).find("td:eq(0)").text();
  10. objOrderDetailViewModel.Quantity = $(this).find("td:eq(3)").text();
  11. objOrderDetailViewModel.Total = $(this).find("td:eq(5)").text();
  12. objOrderDetailViewModel.Discount = $(this).find("td:eq(4)").text();
  13. objOrderDetailViewModel.UnitPrice = $(this).find("td:eq(2)").text();
  14. ListOrderDetailViewModel.push(objOrderDetailViewModel);
  15. });
  16. objOrderViewModel.listOrderDetailViewModel = ListOrderDetailViewModel;
  17. $.ajax({
  18. async: true,
  19. type: 'POST',
  20. dataType: 'JSON',
  21. contentType: 'application/json; charset=utf-8',
  22. data: JSON.stringify(objOrderViewModel),
  23. url: '/home/Index',
  24. success: function (data) { alert(data) },
  25. error: function () { alert('There is some problem to get the unit price.') }
  26. });
  27. ResetItems();
  28. ResetItemsDetails();
  29. }

My Model

  1. public class OrderViewModel
  2. {
  3. public int OrderId { get; set; }
  4. public int PaymentTypeId { get; set; }
  5. public int CustomerId { get; set; }
  6. public string OrderNumber { get; set; }
  7. public DateTime OrderDate { get { return DateTime.Now; } }
  8. public decimal FinalTotal { get; set; }
  9. public IEnumerable<OrderDetailViewModel> listOrderDetailViewModel { get; set; }
  10. }
  1. public class OrderDetailViewModel
  2. {
  3. public int OrderDetailId { get; set; }
  4. public int ItemId { get; set; }
  5. public decimal Quantity { get; set; }
  6. public decimal Total { get; set; }
  7. public decimal UnitPrice { get; set; }
  8. }

谢谢!

英文:

I want to submit my form data, but before I wanted to check my query and get the data in the
objOrderViewModel
I checked with debugger, no data comes, only DateTime, what could be the problem?

Controller

  1. [HttpPost]
  2. public JsonResult Index(OrderViewModel objOrderViewModel)
  3. {
  4. return Json(data: "");
  5. }

My js

  1. var objOrderViewModel = {};
  2. var ListOrderDetailViewModel = new Array();
  3. objOrderViewModel.PaymentTypeId = $("#PaymentType").val();
  4. objOrderViewModel.CustomerId = $("#Customer").val();
  5. objOrderViewModel.FinalTotal = $("#txtFinalTotal").val();
  6. $("#tblRestaurantItemList").find("tr:gt(0)").each(function () {
  7. var objOrderDetailViewModel = {};
  8. objOrderDetailViewModel.ItemId = $(this).find("td:eq(0)").text();
  9. objOrderDetailViewModel.Quantity = $(this).find("td:eq(3)").text();
  10. objOrderDetailViewModel.Total = $(this).find("td:eq(5)").text();
  11. objOrderDetailViewModel.Discount = $(this).find("td:eq(4)").text();
  12. objOrderDetailViewModel.UnitPrice = $(this).find("td:eq(2)").text();
  13. ListOrderDetailViewModel.push(objOrderDetailViewModel);
  14. });
  15. objOrderViewModel.listOrderDetailViewModel = ListOrderDetailViewModel;
  16. $.ajax({
  17. async: true,
  18. type: 'POST',
  19. dataType: 'JSON',
  20. contentType: 'application/json; charset=utf-8',
  21. data: JSON.stringify(objOrderViewModel),
  22. url: '/home/Index',
  23. success: function (data) { alert(data) },
  24. error: function () { alert('There is some problem to get the unit price.') }
  25. });
  26. ResetItems();
  27. ResetItemsDetails();
  28. }

My Model

  1. public class OrderViewModel
  2. {
  3. public int OrderId { get; set; }
  4. public int PaymentTypeId { get; set; }
  5. public int CustomerId { get; set; }
  6. public string OrderNumber { get; set; }
  7. public DateTime OrderDate { get { return DateTime.Now; } }
  8. public decimal FinalTotal { get; set; }
  9. public IEnumerable<OrderDetailViewModel> listOrderDetailViewModel { get; set; }
  10. }
  1. public class OrderDetailViewModel
  2. {
  3. public int OrderDetailId { get; set; }
  4. public int ItemId { get; set; }
  5. public decimal Quantity { get; set; }
  6. public decimal Total { get; set; }
  7. public decimal UnitPrice { get; set; }
  8. }

Thanks a lot

答案1

得分: 1

This answer assumes you have [AutoValidateAntiforgeryToken] in your controller.

You need to submit the anti-forgery token, since it is a POST. POSTS require the anti-forgery token when validation tags are applied in the controller. In AJAX, you need to add the token manually.

At the top of your CSHTML file, add this line: @Html.AntiForgeryToken(). This is your manual anti-forgery token.

You may want to make this JavaScript function for future convenience.

  1. function GetToken() {
  2. return document.querySelector("[name='__RequestVerificationToken']").value;
  3. }

Add a parameter to your data that you send through AJAX. This parameter should be named __RequestVerificationToken and its value should be GetToken(), which is the value of the anti-forgery token, coming from the JavaScript function above.

英文:

EDIT: I forgot to put single quotes around the __RequestVerificationToken in the GetToken() function. They are there now.

This answer assumes you have [AutoValidateAntiforgeryToken] in your controller.

You need to submit the anti-forgery token, since it is a POST. POSTS require the anti-forgery token when validation tags are applied in the controller. In AJAX, you need to add the token manually.

At the top of your CSHTML file, add this line: @Html.AntiForgeryToken(). This is your manual anti-forgery token.

You may want to make this JavaScript function for future convenience.

  1. function GetToken() {
  2. return document.querySelector("[name='__RequestVerificationToken']").value;
  3. }

Add a parameter to your data that you send through AJAX. This parameter should be named __RequestVerificationToken and its value should be GetToken(), which is the value of the anti-forgery token, coming from the JavaScript function above.

huangapple
  • 本文由 发表于 2023年7月18日 02:31:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定