.Net core - Ajax post does not pass variables to controller method

sjohn285 :

When I set a breakpoint on LoadReport, every parameter is null. For some reason the values are not binding to the parameters with the same name.

Javascript/AJAX

$('#savedCriteria').on('change', function () {
    var criteriaSelected = $('#savedCriteria option:selected').text();
    var data = { actionName: "Daily", reportInput: "ReportDaily", reportCriteria: criteriaSelected };
    //Ajax form post
    $.ajax({
        type: 'POST',
        data: data,
        contentType: "application/json; charset=utf-8",
        url: '@Url.Action("LoadReport", ViewContext.RouteData.Values["Controller"].ToString())',
        success: function (data) {
            if (data.success) {
                alert("Test");
            } else {
                alert("Test Not Successful");
            }
        }
    });
});

Controller

public void LoadReport(string actionName, string reportInput, string reportCriteria)
{
    var reportObject = Activator.CreateInstance(Type.GetType(reportInput));
    IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(reportInput);
    RedirectToAction(actionName, "Reports", reportList.Where(x => x.CriteriaName == reportCriteria));
}
jojess :

Default method type is HttpGet, you need to set it to HttpPost.

[HttpPost]
public void LoadReport(string actionName, string reportInput, string reportCriteria)
{
    var reportObject = Activator.CreateInstance(Type.GetType(reportInput));
    IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(reportInput);
    RedirectToAction(actionName, "Reports", reportList.Where(x => x.CriteriaName == reportCriteria));
}

Also keep in mind that with your ajax call you can not use RedirectToAction. You need something like this:

[HttpPost]
public ActionResult  LoadReport(string actionName, string reportInput, string reportCriteria)
{
    var reportObject = Activator.CreateInstance(Type.GetType(reportInput));
    IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(reportInput);
    Return Json(Url.Action(actionName, "Reports", reportList.Where(x => x.CriteriaName == reportCriteria));
}

And in your ajax call:

success: function (data) {
   window.location.href = data;
   }

UPDATE: you also need to create a POCO object and add that to the HttpPost method as parameter instead of separate parameters. Also [FromBody] attribute is needed.

POCO:

public class Data
{
    public string actionName { get; set; }
    public string reportInput { get; set; }
    public string reportCriteria { get; set; }

}

Controller:

[HttpPost]
public JsonResult LoadReport([FromBody]Data data)
{
    var reportObject = Activator.CreateInstance(Type.GetType(data.reportInput));
    IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(data.reportInput);
    return Json(Url.Action(data.actionName, "Reports"));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31995&siteId=1