2

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?

0

1 Answer 1

1

If you wish to use an AJAX call, you can use a simple AJAX that sends your data to your Controller. This will send data to your Controller Method (subtmitUserAnswer) and return data (response), you can populate the page with that data.

If you wish to redirect to various pages/views you should not use AJAX, as RedirectToAction() as a C# ActionResult will not function through AJAX. You can move pages using window.location.href in the success handler, however if you wish to direct to various views depending on answers, this best suited for passing a Model to your Controller and using MVC properly. This is basic MVC functionality and further information can be found here or on one of the many tutorials online.

Here is the AJAX call you would need if you wish to async. call data/logic from your backend and stay on the same page.

var dateInput = "";

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
    var dateInput = (month + '/' + day + '/' + year);

    $.ajax({
        type: "POST",
        url: "~/Controllers/controllerName/submitUserAnswer", //Put your path here.
        data: { userOutput : dateInput }
        success: function (response) {
            //Success! Utilize the data sent back in "response" here
    }
    //add Error handling here
    });
}
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.