1

Hello i am trying to make ASP.NET MVC parse a request with json in the data to a model, but for some reason it just won't work.

Here is my request

        $.ajax({
            type: "POST",
            url: "/ajax/album/SaveAlbumImages/",
            data: { model: ko.toJSON(model) },
            dataType: "json",
            success: function (data: BaseJson<string>, textStatus, jqXHR) {

            }
        });

enter image description here

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:2685
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ASP.NET_SessionId=gtjnl1hr2st3szm50mwmlmb4; __RequestVerificationToken=AWXbN4FYcsQX4Wt-bWXTtCWmPAtBf_5ShztUtZnh6DQ552xLiZoUbsAtCsQJXYly7zF3F6nJNYaKJbu5QTucMrD1sv_vX2qj9Y0Db4OOksg1; .ASPXAUTH=677B1CD5D4AE1BF1B500EED6D9E465B79F7068187B1018671FCAA38E518DE12E93B9C082E27CB39C833DFC53FFBC78099D97F72E6E388A3895C1961FB75E5FDC0DCCE4210201D85866C7F59384135D2B; Cosplay.locale=da-DK
Host:localhost:52972
Origin:http://localhost:52972
Referer:http://localhost:52972/album/managealbumimagesnew/1221
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
model:{"AlbumId":1221,"Images":[{"ImageId":2454,"Description":null},{"ImageId":2455,"Description":null},{"ImageId":2456,"Description":null},{"ImageId":2457,"Description":null},{"ImageId":2458,"Description":null},{"ImageId":2459,"Description":null},{"ImageId":2460,"Description":null},{"ImageId":2461,"Description":null},{"ImageId":2462,"Description":null},{"ImageId":2463,"Description":null},{"ImageId":2464,"Description":null},{"ImageId":2465,"Description":null},{"ImageId":2466,"Description":null},{"ImageId":2467,"Description":null},{"ImageId":2468,"Description":null},{"ImageId":2469,"Description":null},{"ImageId":2470,"Description":null},{"ImageId":2471,"Description":null},{"ImageId":2472,"Description":null},{"ImageId":2473,"Description":null},{"ImageId":2474,"Description":null},{"ImageId":2475,"Description":null},{"ImageId":2476,"Description":null},{"ImageId":2477,"Description":null},{"ImageId":2478,"Description":null},{"ImageId":2479,"Description":null},{"ImageId":2480,"Description":null},{"ImageId":2481,"Description":null},{"ImageId":2482,"Description":null},{"ImageId":2483,"Description":null},{"ImageId":2484,"Description":null},{"ImageId":2485,"Description":null},{"ImageId":2486,"Description":null},{"ImageId":2487,"Description":null},{"ImageId":2488,"Description":null},{"ImageId":2489,"Description":null},{"ImageId":2490,"Description":null},{"ImageId":2491,"Description":null},{"ImageId":2492,"Description":null},{"ImageId":2493,"Description":null},{"ImageId":2494,"Description":null},{"ImageId":2495,"Description":null},{"ImageId":2496,"Description":null},{"ImageId":2497,"Description":null},{"ImageId":2498,"Description":null},{"ImageId":2499,"Description":null},{"ImageId":2500,"Description":null}]}

This is my Code in MVC

public ActionResult SaveAlbumImages(AlbumImagesSaveModel model)
        {

And the model itself

public class AlbumImagesSaveModel
{
    public int AlbumId { get; set; }
    public List<AlbumImageSaveModel> Images { get; set; } 
}
public class AlbumImageSaveModel
{
    public int ImageId { get; set; }
    public string Description { get; set; }
}

What am i doing wrong?

1 Answer 1

5

You are sending JSON, so specify that in the Content-Type request header, otherwise the server couldn't know your intentions:

$.ajax({
    type: 'POST',
    url: '/ajax/album/SaveAlbumImages/',
    contentType: 'application/json',
    data: ko.toJSON(model),
    success: function (data, textStatus, jqXHR) {

    }
});

Also notice how we are wrapping the entire data parameter to send the JSON, no need to be decoupling it. I have also removed the dataType: 'json' parameter as if your server sends appropriate Content-Type response header (which is what ASP.NET MVC does if you return a JsonResult from your action), jQuery is able to automatically infer the response type and feed the correct type to your success callback.

Also you had some really pretty broken javascript syntax for your callback. You seem to have attempted to provide some type to the data argument of the success function which is invalid javascript.

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

3 Comments

Thank you that worked! The reason for my javascript syntax is because i used Typescript
Well, if you use TypeScript or some other language, please specify that in your question or use the appropriate tags.
ofcause, just forgot to remove that part of the typescript.

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.