英文:
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
[HttpPost]
public JsonResult Index(OrderViewModel objOrderViewModel)
{
return Json(data: "");
}
My js
function FinalPayment() {
var objOrderViewModel = {};
var ListOrderDetailViewModel = new Array();
objOrderViewModel.PaymentTypeId = $("#PaymentType").val();
objOrderViewModel.CustomerId = $("#Customer").val();
objOrderViewModel.FinalTotal = $("#txtFinalTotal").val();
$("#tblRestaurantItemList").find("tr:gt(0)").each(function () {
var objOrderDetailViewModel = {};
objOrderDetailViewModel.ItemId = $(this).find("td:eq(0)").text();
objOrderDetailViewModel.Quantity = $(this).find("td:eq(3)").text();
objOrderDetailViewModel.Total = $(this).find("td:eq(5)").text();
objOrderDetailViewModel.Discount = $(this).find("td:eq(4)").text();
objOrderDetailViewModel.UnitPrice = $(this).find("td:eq(2)").text();
ListOrderDetailViewModel.push(objOrderDetailViewModel);
});
objOrderViewModel.listOrderDetailViewModel = ListOrderDetailViewModel;
$.ajax({
async: true,
type: 'POST',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(objOrderViewModel),
url: '/home/Index',
success: function (data) { alert(data) },
error: function () { alert('There is some problem to get the unit price.') }
});
ResetItems();
ResetItemsDetails();
}
My Model
public class OrderViewModel
{
public int OrderId { get; set; }
public int PaymentTypeId { get; set; }
public int CustomerId { get; set; }
public string OrderNumber { get; set; }
public DateTime OrderDate { get { return DateTime.Now; } }
public decimal FinalTotal { get; set; }
public IEnumerable<OrderDetailViewModel> listOrderDetailViewModel { get; set; }
}
public class OrderDetailViewModel
{
public int OrderDetailId { get; set; }
public int ItemId { get; set; }
public decimal Quantity { get; set; }
public decimal Total { get; set; }
public decimal UnitPrice { get; set; }
}
谢谢!
英文:
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
[HttpPost]
public JsonResult Index(OrderViewModel objOrderViewModel)
{
return Json(data: "");
}
My js
var objOrderViewModel = {};
var ListOrderDetailViewModel = new Array();
objOrderViewModel.PaymentTypeId = $("#PaymentType").val();
objOrderViewModel.CustomerId = $("#Customer").val();
objOrderViewModel.FinalTotal = $("#txtFinalTotal").val();
$("#tblRestaurantItemList").find("tr:gt(0)").each(function () {
var objOrderDetailViewModel = {};
objOrderDetailViewModel.ItemId = $(this).find("td:eq(0)").text();
objOrderDetailViewModel.Quantity = $(this).find("td:eq(3)").text();
objOrderDetailViewModel.Total = $(this).find("td:eq(5)").text();
objOrderDetailViewModel.Discount = $(this).find("td:eq(4)").text();
objOrderDetailViewModel.UnitPrice = $(this).find("td:eq(2)").text();
ListOrderDetailViewModel.push(objOrderDetailViewModel);
});
objOrderViewModel.listOrderDetailViewModel = ListOrderDetailViewModel;
$.ajax({
async: true,
type: 'POST',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(objOrderViewModel),
url: '/home/Index',
success: function (data) { alert(data) },
error: function () { alert('There is some problem to get the unit price.') }
});
ResetItems();
ResetItemsDetails();
}
My Model
public class OrderViewModel
{
public int OrderId { get; set; }
public int PaymentTypeId { get; set; }
public int CustomerId { get; set; }
public string OrderNumber { get; set; }
public DateTime OrderDate { get { return DateTime.Now; } }
public decimal FinalTotal { get; set; }
public IEnumerable<OrderDetailViewModel> listOrderDetailViewModel { get; set; }
}
public class OrderDetailViewModel
{
public int OrderDetailId { get; set; }
public int ItemId { get; set; }
public decimal Quantity { get; set; }
public decimal Total { get; set; }
public decimal UnitPrice { get; set; }
}
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.
function GetToken() {
return document.querySelector("[name='__RequestVerificationToken']").value;
}
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.
function GetToken() {
return document.querySelector("[name='__RequestVerificationToken']").value;
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论