英文:
C# ASP.NET MVC - Ajax in .cshtml fails to send data to controller
问题
我有一个.cshtml页面,它构建了一个数据数组,在控制台中看起来像这样:
JSON.stringify(obj) = [{"ParcelKey":8911,"ParcelStatus":"Check"},{"ParcelKey":8912,"ParcelStatus":"Check"}]
我有一个类似这样的Ajax代码:
$.ajax(
{
type: "POST", //HTTP POST Method
url: "/Parcel/BulkParcelStatusUpdateSave", // Controller/Action
data: JSON.stringify(obj),
traditional: false,
cache: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
const myArray = data.split("|")
$('#Parcel_Save_Success').removeClass('hidden');
$('#CaseStatus').val(myArray[0]);
$("#DetailedParcelGrid").data("kendoGrid").dataSource.read();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error While Saving Parcel Details. Status: " + textStatus); alert("Error: " + errorThrown);
}
});
我有一个模型,看起来像这样:
public class ParcelBulkUpdateSaveModel
{
public int ParcelKey { get; set; }
public string ParcelStatus { get; set; }
}
在ParcelController中,我有以下内容:
[HttpPost]
public string BuldParcelStatusUpdateSave(List
{
string returnString = "";
foreach (var record in sentData)
{
_repo.BulkParcelStatusUpdateSave(record);
}
return returnString;
}
我在控制器的foreach语句上设置了断点。
当我运行应用程序时,Ajax执行错误,我收到了2个警报。
为什么Ajax调用无法连接到控制器?
感谢任何帮助。
英文:
I have a .cshtml page which builds an array of data which looks like this in the console.log:
JSON.stringify(obj) = [{"ParcelKey":8911,"ParcelStatus":"Check"},{"ParcelKey":8912,"ParcelStatus":"Check"}]
I have Ajax code which looks like this:
$.ajax(
{
type: "POST", //HTTP POST Method
url: "/Parcel/BulkParcelStatusUpdateSave", // Controller/Action
data: JSON.stringify(obj),
traditional: false,
cache: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
const myArray = data.split("|")
$('#Parcel_Save_Success').removeClass('hidden');
$('#CaseStatus').val(myArray[0]);
$("#DetailedParcelGrid").data("kendoGrid").dataSource.read();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error While Saving Parcel Details. Status: " + textStatus); alert("Error: " + errorThrown);
}
});
I have a model which looks like this:
public class ParcelBulkUpdateSaveModel
{
public int ParcelKey { get; set; }
public string ParcelStatus { get; set; }
}
And in the ParcelController, I have the following:
[HttpPost]
public string BuldParcelStatusUpdateSave(List<ParcelBulkUpdateSaveModel> sentData)
{
string returnString = "";
foreach (var record in sentData)
{
_repo.BulkParcelStatusUpdateSave(record);
}
return returnString;
}
I have a breakpoint set on the foreach statement in the controller.
When I run the application, the Ajax executes the error and I get the 2 alerts.
Why is the Ajax call not able to connect to the controller?
Thanks for any assistance.
答案1
得分: 1
你有一个拼写错误。
你正在调用 BulkParcelStatusUpdateSave
函数,
但函数应该是 BuldParcelStatusUpdateSave
。
英文:
You have a typo.
Your calling BulkParcelStatusUpdateSave
when the function is BuldParcelStatusUpdateSave
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论