返回多个列表c#

时间:2013-04-02 11:02:50

标签: c# ajax webmethod

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

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]);

好的 - 几乎就在那里,尝试合并客户端脚本。

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

不确定这是否可行?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

将各种名单包装成更大的类别,即

创建类似

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

并将此包装器返回到您的web方法中,即

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