This is a migrated thread and some comments may be shown as answers.

sending parameters to wcf service for chart

2 Answers 115 Views
Charts
This is a migrated thread and some comments may be shown as answers.
inanc
Top achievements
Rank 1
inanc asked on 04 Jun 2012, 08:08 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 05 Jun 2012, 11:13 AM
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!
0
inanc
Top achievements
Rank 1
answered on 06 Jun 2012, 07:22 AM
Thank you very much Daniel,
It works great now!
This was a real trouble  for me.
Kind Regards
Tags
Charts
Asked by
inanc
Top achievements
Rank 1
Answers by
Daniel
Telerik team
inanc
Top achievements
Rank 1
Share this question
or