2013-04-02 88 views
1

好的。我有幾個JQuery Ajax帖子,這些帖子正在調用單獨的WebMethods。我遇到了一個問題,現在我需要將webmethods合併爲一個,但仍然能夠根據列表拆分返回值。例如,我需要返回多個列表的東西,以便我的Ajax調用可以呈現相應的Jquery Datatables,例如:table 1 = drList [0],table 2 = drList [1]等。返回多個列表c#

Webmethod代碼低於:

[WebMethod] 
[ScriptMethod] 
public static List<GlobalClasses.ValueDateSummary> GetValueDateSummary() 
{ 

    string TSQL = "SELECT * FROM vw_view1 WHERE (SenderBIC ='" + HttpContext.Current.User.Identity.Name + "') ORDER BY ValueDate DESC"; 
    DataTable dtValueDateSummary = null; 
    GlobalClasses.ValueDateSummary objSummary; 

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["MIReporting"].ConnectionString); 
    using (conn) 
    { 
     conn.Open(); 
     using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(TSQL, conn)) 
     { 
      dtValueDateSummary = new DataTable(); 
      sqlAdapter.Fill(dtValueDateSummary); 
     } 
    } 
    List<GlobalClasses.ValueDateSummary> drList = new List<GlobalClasses.ValueDateSummary>(); 
    foreach (DataRow row in dtValueDateSummary.Rows) 
    { 
     objSummary = new GlobalClasses.ValueDateSummary(); 
     objSummary.fld1= row["1"].ToString(); 
     objSummary.fld2 = row["2"].ToString(); 
     objSummary.fld3 = row["3"].ToString(); 
     objSummary.fld5 = row["4"].ToString(); 

     drList.Add(objSummary); 
    } 
    return drList; 
} 

此和其他webmethod調用之間的唯一區別是正在使用的視圖。

Ajax調用是:

  $.ajax({ 
       type: 'POST', 
       url: 'Default.aspx/GetValueDateSummary', 
       data: '{}', 
       contentType: 'application/json;charset=utf-8', 
       dataType: 'json', 
       success: function (response) { 

       renderMsgSummary(response.d); 
       }, 
       error: function (errMsg) { 
        $('#errorMessage').text(errMsg); 
       } 
      }) 

renderMsgSummary是Jquery的數據表,因此這就需要如下:

renderMsgSummary(response.d[0]); 
renderOtherTable(response.d[1]); 

OK - 幾乎沒有,試圖合併的客戶端腳本現在。

 <script type="text/javascript"> 
     $(document).ready(function() { 
      function renderMsgVal(result) { 
       var dtMsgValData = []; 
       $.each(result, function() { 
        dtMsgValData.push([ 
         this.SenderBIC, 
         this.ValueDate, 
         this.MessageType, 
         this.Reference 
        ]); 
       }); 

       function renderMsgSummary(result) { 
        var dtMsgSumData = []; 
        $.each(result, function() { 
         dtMsgSumData.push([ 
          this.SenderBIC, 
          this.MessageDate, 
          this.MessageType, 
          this.Count 
         ]); 
        }); 

        $('#tblValueDateSummary').dataTable({ 
         "aaData": dtMsgValData, 

         "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100]] 
         'asStripClasses': null, 
         "iDisplayLength": 10, 
         //"aaSorting": [[0, "asc"]], 
         "bJQueryUI": true, 
         "bFilter": true, 
         "bAutoWidth": false, 
         "bProcessing": true, 
         // "sDom": 'RC<"clear">lfrtip', 
         "sDom": 'RC<"H"lfr>t<"F"ip>', 

         //Scrolling ....... 
         "sScrollY": "250px", 
         "sScrollX": "100%", 
         "sScrollXInner": "100%", 
         "bScrollCollapse": true, 

         //Dynamic Language ....... 
         "oLanguage": { 
          //"sZeroRecords": "There are no messages that match your, 
          "sLengthMenu": "Display _MENU_ records per, 
          "sInfo": "Displaying _START_ to _END_ of _TOTAL_ records", 
          "sInfoEmpty": "Displaying _START_ to _END_ of _TOTAL_m 
          "sInfoFiltered": "(filtered from _MAX_ total records)", 
          "sEmptyTable": 'No Rows to display.....!', 
          "sSearch": "Search all columns:", 
          "sLoadingRecords": "Please wait - loading..." 
         }, 
         "oSearch": { 
          "sSearch": "", 
          "bRegex": false, 
          "bSmart": true 
         } 
        }); 

        $('#tblMessageDateSummary').dataTable({ 
         "aaData": dtMsgSumData, 
         "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100]] 
         "asStripClasses": null, 
         "iDisplayLength": 10, 
         "aaSorting": [[0, "asc"]], 
         "bJQueryUI": true, 
         "bFilter": true, 
         "bAutoWidth": true, 
         "bProcessing": true, 
         "sDom": 'RC<"clear">lfrtip', 
         //"sDom": '<"F"ip>l', 


         //Scrolling ....... 
         "sScrollY": "250px", 
         "sScrollX": "100%", 
         "sScrollXInner": "100%", 
         "bScrollCollapse": true, 

         //Dynamic Language ....... 
         "oLanguage": { 
          // "sZeroRecords": "There are no messages that match your, 
          "sLengthMenu": "Display _MENU_ records per, 
          "sInfo": "Displaying _START_ to _END_ of _TOTAL_ records", 
          "sInfoEmpty": "Showing 0 to 0 of 0 records", 
          "sInfoFiltered": "(filtered from _MAX_ total records)", 
          "sEmptyTable": 'No Rows to display.....!', 
          "sSearch": "Search all columns:" 
         }, 
         "oSearch": { 
          "sSearch": "", 
          "bRegex": false, 
          "bSmart": true 
         } 
        }); 
       } 


       $.ajax({ 
        type: 'POST', 
        url: 'Default.aspx/GetMessageDetails', 
        data: '{}', 
        contentType: 'application/json;charset=utf-8', 
        dataType: 'json', 
        success: function (response) { 
         //console.log(response); 
         //alert(response.d); 
         renderMsgVal(response.d[0]); 
         renderMsgSummary(response.d[1]); 
        }, 
        error: function (errMsg) { 
         $('#errorMessage').text(errMsg); 
        } 
       }); 


     </script> 

不知道這是否可能?

回答

0

包裝你的各種列表成爲一個更大的類即

使一類像

public class Wrapper 
{ 
    public List<GlobalClasses.ValueDateSummary> list1 {get;set;} 
    public List<GlobalClasses.ValueDateSummary> list2 {get;set;} 
    public List<SomeOtherClassCollectino> list3{get;set;} 
} 

,並在您的WebMethod返回這個包裝即

[WebMethod] 
[ScriptMethod] 
public static List<GlobalClasses.ValueDateSummary> GetValueDateSummary() 
{ 
    // your db logic 
    Wrapper wrapper = new Wrapper(); 

    List<GlobalClasses.ValueDateSummary> drList = new List<GlobalClasses.ValueDateSummary>(); 
    foreach (DataRow row in dtValueDateSummary.Rows) 
    { 
     objSummary = new GlobalClasses.ValueDateSummary(); 
     objSummary.fld1= row["1"].ToString(); 
     objSummary.fld2 = row["2"].ToString(); 
     objSummary.fld3 = row["3"].ToString(); 
     objSummary.fld5 = row["4"].ToString(); 

     drList.Add(objSummary); 
    } 
    wrapper.list1 = drList; 
    //similarly fetch other lists 
    //wrapper.list2 = drList2; 
    return new JavaScriptSerializer().Serialize(wrapper); 
} 

,並在客戶端上你可以像訪問它:

var jsonData = $.parseJSON(msg.d); 
renderMsgSummary(jsonData.list1); 
renderMsgSummary(jsonData.list2); 
1

如果我理解正確,您可以返回List of Lists

public static List<List<GlobalClasses.ValueDateSummary>> GetValueDateSummary()  
{ 
    var allLists = new List<List<GlobalClasses.ValueDateSummary>>(); 

    foreach (DataRow row in dtValueDateSummary.Rows) 
    { 
     objSummary = new GlobalClasses.ValueDateSummary(); 
     objSummary.fld1= row["1"].ToString(); 
     objSummary.fld2 = row["2"].ToString(); 
     objSummary.fld3 = row["3"].ToString(); 
     objSummary.fld5 = row["4"].ToString(); 

     drList.Add(objSummary); 
    } 
    allLists.Add(drList); 

    //add other lists to allLists 
    //.. 

    return allLists; 
} 
+0

是的,只需你可以做到這一點:) –

+0

優秀的工作keyboardp :-) – CSharpNewBee