将数据数组从Json传递给wcf函数作为参数

时间:2011-12-02 07:37:36

标签: jquery asp.net ajax wcf json

大家好我正在使用json在wcf服务中传递数据。以下是我的代码。我能够传递数据 ProjectCollection

但我想将数据作为像这样的数组传递

var ProjectCollection = ['new','test','etc'];

var ProjectCollection = "Test";      
  function GetEmployee() {
        Type = "GET";
        Url = "http://localhost:52136/Service1.svc/GetTimesheetEntries";
        DataType = "jsonp";
        Data = {vb: ProjectCollection,vb1: '1'};
        ProcessData = false;
        method = "GetTimesheetEntries";
        CallService();
    }

 function CallService() {
        $.ajax({
            type: Type, //GET or POST or PUT or DELETE verb
            url: Url, // Location of the service
            data: Data, //Data sent to server
            contentType: ContentType, // content type sent to server
            dataType: DataType, //Expected data format from server
            processdata: ProcessData, //True or False
            success: function (msg) {//On Successfull service call
                ServiceSucceeded(msg);
            },
            error: ServiceFailed// When Service call fails
        });
    }

这是我的网络服务功能。所以我的要求是从json获取所有数组数据到这个函数参数。

 public List<WcfService1.Customer> GetTimesheetEntries(string[] vb , string vb1)
    {
        DataClasses1DataContext i = new DataClasses1DataContext();
        //var b = from vb in i.TimeSheetMasters select vb;
        //return b.ToList();

        var list = from time in i.TimeSheetMasters
                   join activity in i.ProjectMasters
                   on time.ProjectId equals activity.ProjectId
                   join res in i.ResourceMasters on time.ResourceId equals res.ResourceId
                   where time.TaskDetails == vb && time.BookHours == vb1
                   select new WcfService1.Customer
                   {
                       RName = res.ResourceName,
                       PName = activity.ProjectTitle
                   };

        return list.ToList();


    }

1 个答案:

答案 0 :(得分:4)

我找到了我的解决方案

  function GetTimeSheet() {
    //webserviceurl[2]
    Type = "GET";
    Url = "http://gtsp12:3030/Service1.svc/GetTimesheetEntries";
    DataType = "jsonp";
    Data = { Projects: JSON.stringify(projectlist), Resources: JSON.stringify(Resourcelist) };
    method = "GetTimesheetEntries";
    CallService1();
}

只需在脚本中创建数组即可。初始化它并在上面的webservice中作为参数传递。