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?

Akbari
  • 2,369
  • 7
  • 45
  • 85
  • 1
    You don't need to `stringify` it - just use `data: jsonObj,` –  May 07 '15 at 03:34
  • In that case, I'll get `Invalid JSON primitive` error. – Akbari May 07 '15 at 03:37
  • Then there is some other issue. Are you sure you have shown all the html between the `
    ` tags. The fact that form data includes `LiIdD=` suggest you haven't (there is no control with the attribute `name="LiIdD"`)
    –  May 07 '15 at 03: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? – Akbari May 07 '15 at 03:45
  • The button should not be a problem, but can you show the full view. –  May 07 '15 at 03:48
  • 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")',` –  May 07 '15 at 03:51

1 Answers1

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>
Sean O'Brien
  • 183
  • 7
  • 2
    Or just omit `contentType:` since the default is `'application/x-www-form-urlencoded; charset=UTF-8'` –  May 07 '15 at 04:00
  • 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. – Akbari May 07 '15 at 04:07
  • 1
    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 O'Brien May 07 '15 at 04:55
  • 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 :) – pastrami01 Nov 27 '15 at 13:08
  • 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? – ahjashish Sep 01 '20 at 20:06