C# ASP.NET MVC – Ajax in .cshtml fails to send data to controller

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

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 sentData)
{
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

huangapple
  • 本文由 发表于 2023年6月30日 01:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583391.html
匿名

发表评论

匿名网友

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

确定