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.