Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

You have a typo.

Your calling BulkParcelStatusUpdateSave
when the function is BuldParcelStatusUpdateSave

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading