1

I'm using serialize and JSON.stringify methods to make an Ajax call to my ASP.NET MVC application. MVC is unable to bind the model.

This is my JS code and strongly-typed view:

<script>
    function saveDetails() {
        jsonObj = $('#rdform').serialize();
        $.ajax({
            method: "POST",
            url: "R/SaveDetail",
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            data: JSON.stringify(jsonObj)
        });
    }
</script>

<form id="rdform">
    <div>
        <div>
            @Html.LabelFor(m => m.LiIdH)
            @Html.TextBoxFor(m => m.LiIdH)
        </div>
        <div>
            @Html.LabelFor(m => m.LiIdR)
            @Html.TextBoxFor(m => m.LiIdR)
        </div>
    </div>
    <input type="button" onclick="saveDetails()" />
</form>

The request's payload looks like this:

"LiIdH=1&LiIdD=&LiIdR=2"

And this is my Action method:

public bool SaveDetail(Detail detail)

Have I missed something?

6
  • 1
    You don't need to stringify it - just use data: jsonObj, Commented May 7, 2015 at 3:34
  • In that case, I'll get Invalid JSON primitive error. Commented May 7, 2015 at 3:37
  • Then there is some other issue. Are you sure you have shown all the html between the <form> tags. The fact that form data includes LiIdD= suggest you haven't (there is no control with the attribute name="LiIdD") Commented May 7, 2015 at 3:44
  • Yes, I'm sure about that. I removed some fields for brevity. But I just noticed that my button is outside, my it's causing the problem? Commented May 7, 2015 at 3:45
  • 2
    Just noticed you have contentType: 'application/json; charset=utf-8', - you need to remove that line (in addition to removing JSON.stringify()). And I strongly suggest you use url '@Url.Action("SaveDetail", "R")', Commented May 7, 2015 at 3:51

1 Answer 1

4

The reason you are running into trouble is because of your use of both serialize and JSON.stringify. form.serialize returns a string value, which when passed to JSON.serialize gets wrapped with an extra pair of quotes. The easiest way for you to invoke your action method would be to remove your call to JSON.stringify and also remove the contentType option of the ajax call and go with the default, as shown below:

<script>
    function saveDetails() {
        $.ajax({
            method: "POST",
            url: "R/SaveDetail",
            dataType: "json",
            data: $('#rdform').serialize()
        });
    }
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Or just omit contentType: since the default is 'application/x-www-form-urlencoded; charset=UTF-8'
Thanks Sean, but I should remove the contentType altogether. I don't know why, but the contentType in you answer causes problem too. But if I remove it, everything works fine.
I believe it is the 'charset....' causing trouble, but you are correct that removing it and just sticking with the default is probably a better option.
Sean - you saved my life! Had the same issue as the poster - found a lot of posts but no real answer. Have tried everything, custom model binder etc. But that the solution is that easy i never would have thought :)
I have a similar issue with the only difference that my model has a list which I am looping over to create a table wrapped in a Html.BeginForm using Html.DispayTextFor to display text for each row but when I am using $("formElem").serialize and sending it as data. Doesn't seem to receive the list in the model populated, any help?

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.