1

好的。我有几个 JQuery Ajax 帖子,它们调用了单独的 WebMethods。我遇到了一个问题,我现在需要将 webmethods 合并为一个,但仍然能够根据列表拆分返回值。因此,例如,我需要返回多个列表的东西,以便我的 Ajax 调用可以呈现相应的 Jquery 数据表,例如:table 1 = drList[0]、table 2 =drList[1] 等。

Web方法代码如下:

[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);
               }
           })

renderMsgSummaryJquery数据表,所以这需要如下:

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

好的 - 快到了,现在正在尝试合并客户端脚本。

     <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>

不确定这是否可能?

4

2 回答 2

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;
}
于 2013-04-02T11:11:10.727 回答
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]
[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);
于 2013-04-02T11:08:55.257 回答