无法使用 Jquery 将数据从视图发送到控制器 Asp.net MVC
Unable to send the data from view to controller Asp.net MVC using Jquery
我正在尝试 post 从我的视图到控制器的数据,但计数始终为 0。我可以在开发人员控制台中看到数据作为请求的一部分传递,但我的操作方法是无法收到它,我无法在这里解决问题。
开发者控制台中的数据:
来自 UI 的数据:
var massagedRawMaterials = [];
for (var i = 0; i < 5; i++) {
massagedRawMaterials.push({
name: 'Raw Material Name',
shortCode: 'Short Code',
price: 'Price'
});
}
//pass json array to controller function to save line items
$.ajax({
url: "/Home/Create",
type: "POST",
data: JSON.stringify({ rawmaterial: massagedRawMaterials }),
contentType: "application/json",
dataType: 'json',
success: function (savingStatus) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
> Model C#:
public class MaterialViewModel
{
public string Name { get; set; }
public string ShortCode { get; set; }
public string Price { get; set; }
}
Controller method:
public ActionResult Create(List<MaterialViewModel> rawmaterial)
{
// some processing
}
For me the code looks okay or I am unable to find the issue here.
试试这个
$.ajax({
url: "/Home/Create",
type: "POST",
data: { rawmaterial: massagedRawMaterials },
success: function (savingStatus) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
我正在尝试 post 从我的视图到控制器的数据,但计数始终为 0。我可以在开发人员控制台中看到数据作为请求的一部分传递,但我的操作方法是无法收到它,我无法在这里解决问题。
开发者控制台中的数据:
var massagedRawMaterials = [];
for (var i = 0; i < 5; i++) {
massagedRawMaterials.push({
name: 'Raw Material Name',
shortCode: 'Short Code',
price: 'Price'
});
}
//pass json array to controller function to save line items
$.ajax({
url: "/Home/Create",
type: "POST",
data: JSON.stringify({ rawmaterial: massagedRawMaterials }),
contentType: "application/json",
dataType: 'json',
success: function (savingStatus) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
> Model C#:
public class MaterialViewModel
{
public string Name { get; set; }
public string ShortCode { get; set; }
public string Price { get; set; }
}
Controller method:
public ActionResult Create(List<MaterialViewModel> rawmaterial)
{
// some processing
}
For me the code looks okay or I am unable to find the issue here.
试试这个
$.ajax({
url: "/Home/Create",
type: "POST",
data: { rawmaterial: massagedRawMaterials },
success: function (savingStatus) {
},
error: function (xhr, ajaxOptions, thrownError) {
}