1

I want to get the value in my controller from ajax. I set a break point in the controller and it breaks, but the value is not there. Am I missing something in my code or do I need to change something?

Here is my controller code:

            [HttpPost]
    public ActionResult SubmitResponse(string responseData)
    {
        string test = responseData;

        return View();

    }

here is my ajax code:

        $( "#dialog-form" ).dialog({
            autoOpen: false,
            height: 500,
            width: 900,
            modal: true,
            buttons: {
                "Submit": function () {

                    var response = $.trim($('#name').val());

                    //responseData = JSON.stringify(responseData);

                    alert('response data = ' + response + '!!!');
                    //alert('YES');
                    $.ajax({
                        url: 'Questions/SubmitResponse',
                        type: 'POST',
                        data: JSON.stringify(response),
                        dataType: 'json',
                        contentType: 'application/json; charset=utf-8',
                        success: function(){
                            alert('success');
                        },
                        error: function(){
                            alert('error buddy');
                        }
                    });
                },

1 Answer 1

4

You need to use the response data in the success callback.

Edit

I've prepared a full example in hopes to clear the confusion. Let's say that in your client code you have something like this:

<button id="testButton" name="testButton">Simulate</button>
<script>
    $(function () {
        var dataToBeSend = {
            test: "This will be appended to the question title !"
        };

        $("#testButton").click(function () {
            $.ajax({
                url: "/Questions/Test",
                type: "post",
                data: JSON.stringify(dataToBeSend),
                dataType: "json",
                contentType: "application/json",
                success: function (question) {
                    alert(question.Title);
                },
                error: function () {
                    alert("Oh noes");
                }
            });
        });
    });
</script>

Note: You can shorten this call by using $.post.

In your QuestionsController:

public class QuestionsController : Controller
{
    [HttpPost]
    public JsonResult Test(string test)
    {
        var question = new Question {Title = "What is the Matrix ? " + test};
        return Json(question);
    }
}

// Will be Serializing this class
public class Question
{
    public string Title { get; set; }
}

Let me know if you need some clarifications on this.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not seeing the data from the controller. I'm trying to pass the string from ajax to the controller.

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.