views:

65

answers:

2

I still can’t seem to get this to work.I’ve tried string arrays object arrays dictionaries. I might be formatting my JSON incorrectly however I am using the labs_JSON plugin. If I remove my data and test the webservice it calls correctly. The : My code looks as follows :

HTML :

<div style="display: block; float: left;">
        <asp:Repeater ID="rptMenuRecipeCategories" runat="server">
            <HeaderTemplate>
                <div class="recipeChecklistHeader">Menu Recipe Categories</div>
                <ul id="menuRecipeCategories" class="connectedSortable">
            </HeaderTemplate>
            <ItemTemplate>
                <li id="item_<%# Container.ItemIndex + 1 %>" recipeCategoryId='<%# Eval("RecipeCategoryID") %>' >
                    <%# Eval("RecipeCategory") %>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>
</div>
<div style="display: block; float: left">
        <asp:Repeater ID="rptAvailableMenuRecipeCategories" runat="server">
            <HeaderTemplate>
                <div class="recipeChecklistHeader">Available Menu Recipes</div>
                <ul id="availabeMenuRecipeCategories" class="connectedSortable">
            </HeaderTemplate>
            <ItemTemplate>
                <li id="item_<%# Container.ItemIndex + 1 %>" recipeCategoryId='<%# Eval("RecipeCategoryID") %>'>
                    <%# Eval("RecipeCategory") %>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>
</div>

JS:

    $(function () {
        $("#menuRecipeCategories, #availabeMenuRecipeCategories").sortable({
            connectWith: '.connectedSortable',
            update: function (ev, ui) {
                //var result = $('#menuRecipeCategories').sortable('toArray');
                //updateSequenceNumber(result);
            }
        }).disableSelection();
    });

//This is currently raised through a button click event function persistRecipeCategoriesToJSON() { var items = $('#menuRecipeCategories').sortable('toArray'); var dictionaryData = ''; var index = 0; var itemArray = new Array();

        for (i = 0; i <= items.length - 1; i++) {
            if (items[i].length == 0)
                continue;

            var item = $('#' + items[i])[0];
            var recipeCategoryId = item.attributes["recipeCategoryId"].nodeValue;
            itemArray[i] = recipeCategoryId;
        }

        persistPositionUsingAjax($.json.encode(itemArray));
    }

    $(document).ready(function () {
        $.ajaxSetup({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    });

    function persistPositionUsingAjax(jsonData) {
        var webServiceURL = '<%=ResolveUrl("~/UserControls/MenuRecipeChecklistService.asmx")%>' + '/UpdateMenuRecipeChecklist';
        $.ajax({
            // This is page.name/MethodName     
            url: webServiceURL,

            // This is the data (method arguments)
            data: jsonData,

            // This is the handler for success.     
            success: function (msg) {
                alert('success');
            }
        });
    }  

JSON :

["1","2"]

Webservice:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string UpdateMenuRecipeChecklist(*INPUT TYPE TO GO HERE*)
        {
            return "";
       }

**Ok guys. I've resolved this issue. It seemed The JSON was being formatted incorrectly with the Json labs plugin. I added this line to my persistRecipeCategoriesToJSON function:

var args = { jsonData: itemArray };
persistPositionUsingAjax($.json.encode(args));

The JSON is then formatted correctly by assigning a name to the object. I then I update the method signature of my webservice to:

public string UpdateMenuRecipeChecklist(string[] jsonData)

and everything then works :) . Thanks to everyone who attempted to help!

A: 

Try this for your web method signature:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string UpdateMenuRecipeChecklist(List<string>)
{ 
   return ""; 
}

EDIT: Have modified to have the parameter as a List of string now, rather than IEnumerable of string.

Also why don't you try passing the object itself and not the encoded string.

Like:

persistPositionUsingAjax(itemArray)
Floyd Pink
Hi Sorry, that did not work. It seems the web service doesn't get called with that signature (of course assigning a variable name to the method signature). However if I remove my data and remove the method the service does get called correctly. I just can't seem to get it to accept my JSON data being passed it. It might be that the json data is not encoded correctly. However I am still looking into this. Thanks for your input though.
Elixir
Just something to add though. System.IEnumerable isn't serializable. Which means it cannot be used in WebMethods.
Elixir
You could try the edits I have made in the answer and that might help...
Floyd Pink
Thanks Floyd. I've resolved this if you look I've edited it. I appreciate all your input though. Is there any way I can mark you up for it even if its not the solution :) ?
Elixir
Glad it is resolved... You could vote up my answer if that helped in some way. :)
Floyd Pink
A: 

Hi guys. This question is now resolved. I've modified the original article to contain the solution at the bottom.

Elixir
*Ok guys. I've resolved this issue. It seemed The JSON was being formatted incorrectly with the Json labs plugin. I added this line to my persistRecipeCategoriesToJSON function:var args = { jsonData: itemArray };persistPositionUsingAjax($.json.encode(args));The JSON is then formatted correctly by assigning a name to the object. I then I update the method signature of my webservice to:public string UpdateMenuRecipeChecklist(string[] jsonData)and everything then works :) . Thanks to everyone who attempted to help!
Elixir