0

I want to be able to convert a JSON object that is passed through ajax(jquery) in my webservice. At the moment I can get it to return the message "Hello World" but I don't know how to access the passed JSON data and then convert to a collection of type IList so I can iterate over the collection. I've had a look around on stackoverflow but I'm getting confused what to do can some one help me.

Here is my code:

jQuery:

var data = { dvals: [{'Name' : 'Acer', 'Count' : '2'}, {'Name' : 'HP', 'Count' : '4'} ] };

function getProducts(json, pageIndex, pageSize) {
    json["PageIndex"] = pageIndex;
    json["PageSize"] = pageSize;

    $.ajax({
        type: 'POST',
        url: '/website2/WebServices/GetProducts.asmx/GetProductsAndFilters',
        data: JSON.stringify(json),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (responseText) {
            //alert(responseText.d);
            $('.console').html(responseText.d);
        },
        error: function (xhr, status, error) {
            //var msg = JSON.parse(xhr.responseText);
            //alert(msg.Message);
            $('.console').html(xhr.responseText)
        }
    });
}
getProducts(data, '0', '2');

My asp.net C#:

public class Filter
{
    public string Name;
    public int Count;
}
public class Product
{
    public int Id;
    public string Title;
    public string ShortDescription;
    public string Brand;
    public string Model;
    public double SellPrice;
    public string DescountPercentage;
    public int Votes;
    public int TotalRating;
    public double Rating
    {
        get
        {
            return Votes / TotalRating;
        }
    }
}

public class FiltersAndProducts
{
    List<Filter> Filters;
    List<Product> Products;
    int PageIndex;
    int PageSize;
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsAndFilters()
{        
    return "Hello World";
}
2
  • 1
    You can construct a javascript object (matching your c# class structure) and pass it to your web service. There is no need to explicitly convert the javascript object to c# object as long as your js's object match your c#'s class structure. Commented Oct 28, 2011 at 4:54
  • Can you give me a e.g of what you're saying I haven't been doing this for a while. I don't know what you're saying. What I want to do is to perform operations based on what filters are passed through and get the products related to those filters. If you gave me an example in my GetProductsAndFilters function I might understand what you're saying Commented Oct 28, 2011 at 5:07

2 Answers 2

3

Note the various ways to call it here: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

Its all about how you define what is going to GetProductsAndFilters. You can pass as a list or pass as a DTO.

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

Comments

2

if you make a class like

public class dvals{

public string Name{get;set;}
public string Count{get;set;}

}

prepare json

var dvals =[{Name:'Acer',Count:'2'},{Name:'HP',Count:'4'}];
dval=JSON.stringify(dvals);

send via ajax

$.ajax({
        type: 'POST',
        url: '/website2/WebServices/GetProducts.asmx/GetProductsAndFilters',
        data: dval,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (responseText) {
            //alert(responseText.d);
              console.log(responseText);
            $('.console').html(responseText.d);
        },
        error: function (xhr, status, error) {
            //var msg = JSON.parse(xhr.responseText);
            //alert(msg.Message);
            $('.console').html(xhr.responseText)
        }
    });

webservice side

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsAndFilters(IList<dvals> dvalsList)
{        
    return "Hello World";
}

1 Comment

I'll give that a try and get back to you later thanks for the mean time. I hope it works if anyone else has a better way leave an answer thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.