So I have been working with an ASP.NET MVC 5 web application, and am doing a survey-style project. Essentially a user is asked a question, I do some formatting, and then I want the data passed back to my controller to serve down the next question in another view. The issue is the passing of data from a javascript variable to a c# variable (method). Here is the code I have:
Javascript:
function validateDateInput() {
var year = $('#yearInput').val();
var month = $('#monthInput').val();
var day = $('#dayInput').val();
//a lot of validation in here to make sure the date is an actual date
goToNextQuestion(month + '/' + day + '/' + year);
}
function goToNextQuestion(output) {
//here is where I need to pass my variable, output, to a c# function in my controller
//I need to call the method submitUserAnswer(output)
}
And my C# code:
public void submitUserAnswer(String userOutput) {
//here I take the answer and feed the user the next question, possibly linking to other c# methods
}
So I initially was going to use [WebMethod], but ran into some issues there (I am a very new to C# and ASP.Net in general, and couldn't find a way to implement it. I can't just pass the variable to ViewBag for obvious reasons, so then I ran across a post which suggested a jQuery Ajax call. I have never used Ajax before. How can I format an Ajax call to do what I want, or is there another, simpler method to do the same thing?