0

So I have created a method on another page of my web app that uses $.Ajax in the Javascript section of my page to retrieve data being returned from a function in my ASP.NET controller for that page/view, and now I am trying to create a new section that will take a parameter being given to the ASP.NET controller from the Javascript script and it will return some data based on the parameter being given.

Here is my Ajax query so far:

function testFunction(){
    let selectedDateJS = getSelectedDate()
    let customGraphData = $.ajax({
        type: "POST",
        url: '@Url.Action("GetCustomGraphData", "Graph")',

        success: console.log("success: "+success),
    })
}

And then here is my controller function in ASP.NET:

public async Task<JsonResult> GetCustomGraphData(string selectedDate)
{
    DateTime selectedDateDT = Convert.ToDateTime(selectedDate);
    // if selectedDate is NOT a working day:
    if (!bh.IsWorkingDay(selectedDateDT)){
        // Do something if the date being selected on the form IS NOT a working day.
        return Json(null);
    }
    else// else, if it IS a working day :
    {
        CustomGraphVM selectedDatedata = await GetCustomData(selectedDateDT);
        return Json(selectedDatedata);
    }

}

I tried using a data: 'myDate', addition to the Ajax query under the url part but it didn't send anything (null was returned on the string selectedDate parameter of my ASP.NET controller function, but it was returned as null.

Also if it makes it easier, I am parsing a date to the controller from Ajax, and the date is in the format 'yyyy-mm-dd'.

The reason why I am doing this in $.Ajax and with Javascript as opposed to doing it with the ASP.NET is because I am trying to make a dynamic update on the view - and this worked great previously with my GET request that just returned a fixed value from the controller.

1 Answer 1

1

If you are passing data via AJAX then you want to match the name you gave your parameter.

function testFunction(){
    
    let selectedDateJS = getSelectedDate();    
    let customGraphData = $.ajax({
        type: "POST",
        url: '@Url.Action("GetCustomGraphData", "Graph")',
        data: {
           selectedDate: selectedDateJs
        }
        success: console.log("success: "+success),
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.