Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sean Dooley 289 posts 528 karma points
    May 16, 2011 @ 14:29
    Sean Dooley
    0

    dynamic dropdown list from /base or webservice?

    Here is the scenario

    I'm creating a form which is dynamically populated depending on what location and date the user selects. I am currently looking at using either /base or a webservice to do this.

    Here is a walkthrough of how I see it working

    1. User selects location (a node in the content structure)
    2. User selects a date
    3. The location and date is sent to /base or webservice
    4. The webservice checks the location and date, and returns the opening hours, which are a property of the location (using the Opening Hours project)
    5. A dropdown list is populated with the opening times

    I am more use to using webservices, but I don't know how to workout what location (node) is being passed - which I have managed to do using /base.

    How would I iterate over the opening hours of a location in a /base class? Or is there another way of achieving what I am trying to achieve?

    Thanks in advance for any responses

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 16, 2011 @ 14:59
    Dirk De Grave
    0

    Hi,

    I'm pretty sure you can load the xml fragment that's stored when using the OpeningHours datatype on a document using

    var doc = XDocument.Parse(string xmlOfProperty);

    and from there on, use Linq to xml to get the xml elements you're after.

     

    Hope this helps.

    Regards,

    /Dirk

  • Sean Dooley 289 posts 528 karma points
    May 16, 2011 @ 16:53
    Sean Dooley
    0

    Thanks Dirk, your reply worked perfectly!

    I am using /base to handle the Ajax events, but it is returning HTML, as follows, rather than JSON

    [{"startTime":"17:00","closingTime":"02:00","hoursOpen":4}]

    Below is the /base code

     

    public static object CalculateDay(string date, string pageId)
    {
    Node node = new Node(Convert.ToInt32(pageId));
            string day = DateTime.Parse(date).DayOfWeek.ToString();
            var json = "";
            switch (day)
            {
            case "Friday":
                        json = JsonConvert.SerializeObject(GetOpeningHours(node, day));
                        break;
                    case "Saturday":
                        json = JsonConvert.SerializeObject(GetOpeningHours(node, day));
                        break;
                    case "Sunday":
                        json = JsonConvert.SerializeObject(GetOpeningHours(node, day));
                        break;
                    default:
                        json = JsonConvert.SerializeObject(GetOpeningHours(node, day));
                        break;
            }
            return json;
    }
    private static object GetOpeningHours(Node node, string day)
    {
    XDocument xmlDoc = XDocument.Parse(node.GetProperty("openingHours").ToString());
            var q = from item in xmlDoc.Descendants("scheduleItem")
            where item.Element("weekDayLocal").Value == day
                    select new
                    {
                    startTime = item.Element("firstSet").Element("hourStart").Value,
                            closingTime = item.Element("firstSet").Element("hourEnd").Value,
                            hoursOpen = 4
                    };
            return q;
    }

    Any ideas why is returning HTML rather than JSON?

     

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 16, 2011 @ 17:03
    Dirk De Grave
    0

    Decorate your base method with the following attribute

    [RestExtensionMethod(returnXml = false)]
    

    and create a helper class to convert whatever object you'd like to return to a json string. Find code snippet on from Sebastiaan's Contour contrib project

    Don't forget to add references for Json.NET

     

    Hope this helps.

    Regards,

    /Dirk

     

Please Sign in or register to post replies

Write your reply to:

Draft