1

This:

        $.ajax({
            url: '/Merchant/SaveDirty',
            type: 'POST',
            dataType: 'json',
            data: ko.toJSON(dirtyItems),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                // get the result and do some magic with it
                //var message = data.Message;
                alert(ko.toJSON(dirtyItems));
            }
        });

...currently calls this:

[HttpPost]
public void SaveDirty(string json)
{

}

...but when I hit the breakpoint in SaveDirty, no data is passed. I've verified that ko.toJSON(dirtyItems) returns a JSON string in the javascript. What am I doing wrong?

Thanks!

  • try data: {json: JSON.stringify(ko.toJSON(dirtyItems))} – David Shorthose Aug 20 '14 at 21:37
  • Dave, this causes my MVC controller method to not get hit. –  Aug 20 '14 at 21:40
  • ok then try it just with the json.stringify section only – David Shorthose Aug 20 '14 at 21:42
  • @Killingsworth, have you tried creating a C# object to model your items? Then your controller could just accept a list or array of these and MVC would handle the binding for you. – Bryan Aug 20 '14 at 21:43
  • I could be wrong, as I've never used .net. However, from quick googling, it seems the parameter of the controller action is for GET variables? Just a thought. – Devin H. Aug 20 '14 at 21:44
  • @Bryan That's my plan; I wanna crawl before I walk though. –  Aug 20 '14 at 21:46
  • @DevinH. Where would you put the parameters in that case? –  Aug 20 '14 at 21:46
  • @DevinH. You can use parameters for either GET or POST. – Daniel Sanchez Aug 20 '14 at 21:47
  • please review the top 2 answers for examples of retrieving unbound values from the form: http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-or – Daniel Sanchez Aug 20 '14 at 21:50
  • Yeah, but there must be some way to post a straight JSON string to a controller action using AJAX. –  Aug 20 '14 at 21:52
  • Try this method? http://stackoverflow.com/questions/17370062/posting-json-data-via-jquery-to-asp-net-mvc-4-controller-action – Daniel Sanchez Aug 20 '14 at 22:23
  • `data: ko.toJSON(dirtyItems),` will return JSON (which is probably a complex object), but your action method parameter is `string`. To test, replace `data: ko.toJSON(dirtyItems),` with `data: {json: 'some value'},` and check if value is bound in the action method –  Aug 20 '14 at 22:36
  • contentType: "application/json", data: JSON.stringify(dirtyItems), – Seminda Aug 21 '14 at 05:07
  • Did you try full URL? – Nick Mehrdad Babaki Aug 21 '14 at 05:54

3 Answers3

1

@KillingsWorth, is there any specific reason for which you are posting a JSON string? If not then, you could create a class corresponding to dirtyitems type and in your controller method you can accept a list of dirtyItems.

Class DirtyItem 
{ // dirty item properties }

[HttpPost]
public void SaveDirty(List<DirtyItem> dirtyItems)
{

}
shwetaOnStack
  • 594
  • 4
  • 13
1

you can use the following:

$.ajax({

        url: '/Merchant/SaveDirty',

        type: 'POST',

        dataType: 'json',

        data: JSON.stringify(dirtyItems),

        contentType: 'application/json; charset=utf-8',

        success: function (data) {
            ///

///

        }

    });

But if you are using knockout.js in your applicantion then you should do the following:

$.ajax({

        url: '/Merchant/SaveDirty',

        type: 'POST',

        dataType: 'json',

        data:JSON.stringify(ko.mapping.toJS(dirtyItems)),

        contentType: 'application/json; charset=utf-8',

        success: function (data) {

            // get the result and do some magic with it

            //var message = data.Message;

            alert(ko.toJSON(dirtyItems));


        }

    });
Veenu Singh
  • 51
  • 1
  • 1
  • 5
  • Something about "data: ko.toJSON(dirtyItems)" being changed to "data:JSON.stringify(ko.mapping.toJS(dirtyItems))" makes the ajax call not even hit my mvc controller. –  Aug 21 '14 at 14:18
  • are you getting any javascript error, (press ctrl+shift+j to check your js error) pls let me know – Veenu Singh Aug 22 '14 at 06:36
  • pls check the url from ajax request is correct or not, you can check it form your browser console, or provide a full url like "http://localhost/Merchant/SaveDirty" just for check, and see is it working? – Veenu Singh Aug 22 '14 at 06:39
0

This should works:

$.ajax({
            url: '@Url.Action("SaveDirty", "Merchant")'
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(dirtyItems),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                // get the result and do some magic with it
                //var message = data.Message;
                alert(ko.toJSON(dirtyItems));
            }
        });
mashet
  • 836
  • 1
  • 10
  • 17