Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Javascript SQL Ajax JSON
Hey guys, Im struggling with this problem and could do with some advice. Ive done some searching and havent found what im looking for so I figured this was the next stop.
 
After removing the unimportant parts my code looks something like this. the Ajax goes to my serverside GetProducts function which returns a serialized string of JSON. Which is a List of items which a Select statement filled from a SQL database.
 
$.ajax({
        type: 'POST',
        url: 'Answer.aspx/GetProducts',
        data: JSON.stringify({ id: ID }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
 
        document.getElementById("hfProducts").value = msg.d;
 
        document.getElementById("ulList").innerHTML = "";
        var data = eval('(' + document.getElementById("hiddenfieldOfProducts").value + ')');
 
        $.each(data, function (i, item) {
        var message = "<li>" + 
                           "<div  style=" + " width:100%;" + ">" +
                           item.ProductName+"  £" + item.ProductPrice+
                            " </div> " + 
                      "</li>  ";
 
         document.getElementById("ulList").innerHTML += message;
         var newcontent = document.createElement('li');
          });
        }
 });
 
At some point in the process before I start displaying the List items in a long unorganised list.Now I want to split up the items and sort the data based off the Category of the product so It would output like this, with the bold text the category:
 
Food:
Chicken pie £4.00
Roast Dinner £7.00
Chilli and rice £5.00
 
Desert
Apple pie £2.00
Ice cream £3.00
 
KitchenWare
Wok £15.00
Pan £10.00
 
You get the idea, currently they come through in the order they were added to the database based off the unique ID, I want to split them up based off the item.Category and display them like above with the Category title above that section.
 
Can I get some advice on what im doing here? I've spent hours today trying to work on this problem without much success. Since C# doesn't allow you to return multiple lists at the same time in a return statement and I don't want to resort to a lot of repeating code I need advice from someone with more experience.#
 
Hopefully I was clear enough, Thanks in advance.
Posted 19-Feb-13 9:39am
Edited 19-Feb-13 9:40am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What are the values in follwoing elements, because I'm confused
- hfProducts
- hiddenfieldOfProducts
  Permalink  
Comments
Phoenix234 at 20-Feb-13 4:24am
   
Oh I apologise, hfProducts was meant to be renamed to hiddenfieldOfProducts.
 
Its a hiddenfield where I store the data the JSON returns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 Richard MacCutchan 320
2 OriginalGriff 280
3 Jared Sanow 145
4 Snehasish_Nandy 115
0 Sergey Alexandrovich Kryukov 5,939
1 OriginalGriff 4,885
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100