0

I have a MVC4 single page website with a form. The loading of the contents is achieve with ajax. I do not know how to get the data out from JSON in C#? Here is my code:

JavaScript:

$("#subnt").click(function (event) {
        event.preventDefault();
        var url = "/Home/Submit";
        $.post(url, $('form[name="cnt_us-frm"]').serialize(), function (data) {
            if (data.Success === true) {
                $("#min-content").hide().load("/Home/PartialSubmit").fadeIn('normal');   // loads the page into 'min-content' section
            }
            else {
                // display error message
            }
        })
    });
});

C#:

[HttpPost]
public JsonResult Submit()
    {
        return Json(new { Success = true, SomeOtherData = "testing" });
    }
3
  • you don't need to change anything JsonResult action method by default returns data in json format Commented Feb 23, 2014 at 3:05
  • Are you asking how to access form data in your controller? Commented Feb 23, 2014 at 3:10
  • No, the other way around. How to retrieve the data from the form (json). Commented Mar 1, 2014 at 23:53

4 Answers 4

1

Please check below working code -

I have used exactly your working code -

    [HttpPost]
    public JsonResult Submit()
    {
        return Json(new { Success = true, SomeOtherData = "testing" });
    }

Then I used following JQuery to hit the above action -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $('#click').click(function (e) {
            $.ajax({
                url: "@Url.Action("Submit")",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (response) {
                    alert(response);
                },
                success: function (data) {
                    if (data.Success == true)
                        alert(data.SomeOtherData);
                }
            });
        });
    });
</script>

<input type="submit" value="click" id="click" />

And as the output I was able to get an alert as shown below -

enter image description here

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

Comments

0

Easiest thing to do is use the superior json.net

[HttpPost]
public string Submit()
{
    var result = new { success = true, someOtherDate = "testing"};
    var json = JsonConvert.SerializeObject(result);
    return json;
}

Comments

0

Your code is ok bu you can add debugger.and open developer tools check your data .

$.post(url, $('form[name="cnt_us-frm"]').serialize(), function (data) {
             debugger;
            if (data.Success === true) {
                $("#min-content").hide().load("/Home/PartialSubmit").fadeIn('normal');   // loads the page into 'min-content' section
            }
            else {
                // display error message
            }

Comments

0

No, the other way around. How to retrieve the data from the form (json).

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.