1

Ok so I have this form in my view:

<form id="MyForm">
<input type="text" name="myinput" />
<button type="submit" />
</form>

I have the following Javascript at the top of my view, which runs when the page loads:

<script type="text/javascript">
    $(document).ready(
        function () {
            $("#MyForm").submit(
                function () {
                    var url = "Home/TestAjax";

                    var dataToSend = $("#MyForm").serialize();
                    alert(dataToSend);

                    $.ajax
                    (
                        {
                            type: "POST",
                            url: url,
                            data: dataToSend,
                            success: function (data) {
                                alert(data);
                            }
                        }
                    );

                    return false;
                }
            );
        }
    );
</script>

The form is being serialised to ajax correctly, as verified by the alert box. Here is my TestAjax controller method:

[HttpPost]
public string TestAjax(string data)
{
    return !string.IsNullOrEmpty(data) ? "Success" : "Failure";
}

The value being returned is Failure, because the AJAX isn't being posted back. What am I doing wrong here?

Thanks

3
  • what model does your view have? Commented Jan 9, 2014 at 11:39
  • Not using any models at the moment, trying to get my head around just serializing a form to JSON, posting it to a controller and returning a value. Do you need to use a model? Commented Jan 9, 2014 at 11:39
  • what alert is shown here alert(dataToSend); Commented Jan 9, 2014 at 11:40

1 Answer 1

6

The name of your input field is myinput not data. So make sure you have consistently named the argument of your action as well:

[HttpPost]
public ActionResult TestAjax(string myinput)
{
    return !string.IsNullOrEmpty(myinput) ? Content("Success") : Content("Failure");
}

When you use $("#MyForm").serialize() this will return myinput=some_value where some_value is obviously the value that the user has entered in this input field.

And if you had 2 input fields in your form:

<form id="MyForm">
    <input type="text" name="foo" />
    <input type="text" name="bar" />
    <button type="submit" />
</form>

you would of course write a view model:

public class MyViewModel
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

that your controller action will take as parameter:

[HttpPost]
public ActionResult TestAjax(MyViewModel model)
{
    return !string.IsNullOrEmpty(model.Foo) ? Content("Success") : Content("Failure");
}

Also please notice that in ASP.NET MVC controller actions should return ActionResults, not strings or whatever.

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

3 Comments

Darin u caught that again ;)
D'oh, I thought the whole form would be serialised to a single JSON string and passed through as data, thanks very much!
I have looked about ten times over the JS and C# code and left the HTML aside but sometimes HTML is much important as the rest... +1

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.