Home > Software engineering >  ASP.NET Core AJAX POST not returning error, however, not saving data
ASP.NET Core AJAX POST not returning error, however, not saving data

Time:08-27

I am using a button OnClick event to try and save a record to a database using AJAX in ASP.NET Core. The function is not returning an error, however, the data is not being saved. I am just trying to test with hard coded data first. A record with AdapollingProjectProcessStatusId = 1 exists in the database.

function SendHtmlEditorValueToController(data) {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("AJAXPost", "LiveAdapollingProjectProcessStatus")',
        contentType: "application/json", 
        data: JSON.stringify({ "id": 1, "status": 'test'}),
        dataType: 'json',
        success: () => {
          console.log("value is sent");
        },
        error: (error) => {
            console.log(JSON.stringify(error));
        }
    });


}

LiveAdapollingProjectProcessStatusController:

    [HttpPost]
    public JsonResult AJAXPost(int id, string status)
    {
        LiveAdapollingProjectProcessStatus processstatus = new LiveAdapollingProjectProcessStatus
        {
            AdapollingProjectProcessStatusId = id,
            AdapollingProjectProcessStatus = status
        };

        //save it in database
        return Json(processstatus);
    }

LiveAdapollingProjectProcessStatus.cs:

    namespace CPSPMO.Models.PMO
    {
        public partial class LiveAdapollingProjectProcessStatus
        {
            public int AdapollingProjectProcessStatusId { get; set; }
            public string AdapollingProjectProcessStatus { get; set; }
        }
    }

Please let me know if you are able to help me with this AJAX Post.

Thanks

CodePudding user response:

Not sure how do you store it to the database, but the way you pass parameter to backend by ajax should be like below:

function SendHtmlEditorValueToController(data) {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("AJAXPost", "LiveAdapollingProjectProcessStatus")',
        //contentType: "application/json",      //remove this... 
        data:{ "id": 1, "status": 'test'},      //modify here...
        dataType: 'json',
        success: () => {
            console.log("value is sent");
        },
        error: (error) => {
            console.log(JSON.stringify(error));
        }
    });
}

CodePudding user response:

After reviewing the comments regarding missing code for saving the data in the database, I modified the controller:

    [HttpPost]
    public JsonResult AJAXPost(int id, string status)
    {
        LiveAdapollingProjectProcessStatus processstatus = new LiveAdapollingProjectProcessStatus
        {
            AdapollingProjectProcessStatusId = id,
            AdapollingProjectProcessStatus = status
        };

        //save it in database
        var result = _context.LiveAdapollingProjectProcessStatuses.Update(processstatus);
        _context.SaveChanges();
        return Json(processstatus);
    }

It is saving the data to the database now. Thanks for the help

  • Related