sending parameters to wcf service for chart

3 posts, 1 answers
  1. inanc
    inanc avatar
    17 posts
    Member since:
    Mar 2009

    Posted 04 Jun 2012 Link to this post

    Hi,
    I have a problem with the chart component.
    I have a wcf service for retrieving data for the kendo ui chart.
    <script type="text/javascript">
           $(document).ready(function () {
               $("#chart").kendoChart({
                   dataSource: {
                       transport: {
                           read: {
                               type:"post",
                               url:"http://localhost:1038/Services/ProductService.svc/jIlBazliAnaliz",
                               dataType: "json",
                               data: '{BasTar:"2011-01-01",SonTar:"2011-12-31"}'
                               }
                       }
                   },
                   seriesDefaults: {
                       type: "column"
                   },
                   series: [{
                       field: "Sayi",
                       name: "Sayi"
                   }],
                   categoryAxis: {
                       field: "IlAdi",
                       labels: {
                           rotation:-90
                       }
                   }
               });
           });
     
       </script>

    I just would like to transfer two parameters by using the line ;

     data: '{BasTar:"2011-01-01",SonTar:"2011-12-31"}'

    and my service side is 
    in IProductService.cs
    [OperationContract]
            [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "jIlBazliAnaliz")]
            IList<ModelIlBazliAnaliz> jIlBazliAnaliz(DateTime BasTar, DateTime SonTar);
    and in ProductService.svc.cs
     public IList<ModelIlBazliAnaliz> jIlBazliAnaliz(DateTime BasTar, DateTime SonTar)
            {
                IList<ModelIlBazliAnaliz> Iller=new List<ModelIlBazliAnaliz>();
                using (mxKalibrasyonEntities client = new mxKalibrasyonEntities())
                {
                    var bak = client.kal_IlBazliAnaliz(BasTar, SonTar).ToList();
                    foreach (var kalIlBazliAnalizResult in bak)
                    {
                        Iller.Add(new ModelIlBazliAnaliz
                                      {
                                          Sayi = kalIlBazliAnalizResult.Sayi.Value,
                                          Oran = kalIlBazliAnalizResult.Oran.Value,
                                          IlAdi = kalIlBazliAnalizResult.IlAdi
                                      });
                    }
                    return Iller;
                }
            }

    The issue is I am getting always null (0001-01-01) date value in the  ProductService.svc.cs .

    If ı don't use any parameters, I mean with the

    public IList<ModelIlBazliAnaliz> jIlBazliAnaliz() ;

    without any parameters , my chart works great. But it is an obligation to have the chart filled for some time intervals. 


    I wonder how can I send the BasTar and SonTar datetime parameters to the wcf service? I think this is possible.
    Kind regards.
    Inanc
  2. Answer
    Daniel
    Admin
    Daniel avatar
    2219 posts

    Posted 05 Jun 2012 Link to this post

    Hello Inanc,

    You should make the following modifications in order to send the dates as parameters to the service:
    1. Convert the dates to the Microsoft date format.
    2. Specify the content type of the request.
    3. Use the transport parameterMap function to convert the object to JSON.
    4. In the service method change the BodyFormat to wrapped.
    For example:
    transport: {
        read: {
            type: "POST",
            url:"http://localhost:1038/Services/ProductService.svc/jIlBazliAnaliz",
            dataType: "json",
            contentType: "application/json; charset=utf-8"
        },
        parameterMap: function (options, operation) {
            var  data  = {
                BasTar: dateToWcfFormat("2011-01-01"),
                SonTar: dateToWcfFormat("2011-12-31")
            }
     
            return JSON.stringify(data);
        }
    }
     
    ....
     
    function dateToWcfFormat(dateString) {
        var date = new Date(dateString);
        var parsedDate = '\/Date(' + date.getTime() + '-0000)\/';
        return parsedDate;
    }
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "jIlBazliAnaliz")]
    IList<ModelIlBazliAnaliz> jIlBazliAnaliz(DateTime BasTar, DateTime SonTar);


    Kind regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. inanc
    inanc avatar
    17 posts
    Member since:
    Mar 2009

    Posted 06 Jun 2012 Link to this post

    Thank you very much Daniel,
    It works great now!
    This was a real trouble  for me.
    Kind Regards
Back to Top