使用jQuery将JavaScript数组传递给服务器端函数

时间:2013-08-02 21:05:46

标签: c# jquery asp.net ajax

我想执行以下步骤:

  1. 获取复选框值列表,并将它们放入JavaScript中的字符串数组中。
  2. 获取阵列并将其发送到服务器端功能。
  3. 使用阵列服务器端并将其转换为DataTable。
  4. 获取DataTable并将其作为TVP发送到存储过程。
  5. 我已经从服务器端开始工作了。我遇到麻烦的地方是从JavaScript到服务器。

    使用我当前的代码,我收到此错误:

      

    结构化类型中没有足够的字段。结构化类型必须至少有一个字段。

    如何将JavaScript数组传递给Web方法?

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    using System.Web.Script.Services;
    using System.Web.Services;
    
    
        namespace SendTVP
        {
            public partial class _default : System.Web.UI.Page
            {
                //page variables
                string filterList = string.Empty;
                DataTable dt = new DataTable();
                protected void Page_Load(object sender, EventArgs e)
                {
    
                }
                protected void Button1_Click(object sender, EventArgs e)
                {
    
                    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
                    using (var con = new SqlConnection(cs))
                    {
                        using (var cmd = new SqlCommand("spFilterPatientsByRace",con))
                        {
                            con.Open();
                            cmd.CommandType = CommandType.StoredProcedure;
                            SqlParameter param = new SqlParameter();
                            //required for passing a table valued parameter
                            param.SqlDbType = SqlDbType.Structured;
                            param.ParameterName = "@Races";
                            //adding the DataTable
                            param.Value = dt;
                            cmd.Parameters.Add(param);
                            GridView1.DataSource = cmd.ExecuteReader();
                            GridView1.DataBind();
                        }                
                    }
                }
                [ScriptMethod]
                [WebMethod]
                //this method will take JS array as a parameter and turn it into a DataTable
    
                public void TableFilter(string filterList)
                {
                    DataTable filter = new DataTable();
                    filter.Columns.Add(new DataColumn() { ColumnName = "Races" });
                    dt.Columns.Add(new DataColumn() { ColumnName = "Races" });
                    foreach (string s in filterList.Split(','))
                    {
                        filter.Rows.Add(s);
                    }
                    dt = filter;
                }
            }
    
    }
    

    如果这是一种完整的asinine方式,那么修订非常受欢迎:)

2 个答案:

答案 0 :(得分:1)

这里的问题是您的网络表单上有两种方法需要回发周期。

  • 当您调用WebMethod时,它将创建_default类的实例并设置dt变量,但不执行任何操作。
  • 当您致电Button1_Click时,会创建_default类的新实例,因此dt变量设置为new DataTable();。因此,您的参数没有列或其他数据。

您希望在一种方法中处理DataTable,大概是TableFilter方法。

答案 1 :(得分:0)

我建议使用JSON(javascript的一个子集)来表示您传递的数据。 JSON的一个例子是:

[ "string1", "string2" ]

{ "property1": "a", "property2": "b" }

第一个是有效的javascript数组,第二个是有效的javascript对象,直接在<script>标记中传递给浏览器。 JQuery有各种处理JSON的方法,您可以在.NET端查看JSON.NETServiceStack.Text,以便将JSON反序列化为C#对象。

[编辑]

虽然我应该指出,很难将JSON反序列化为C#DataTable,因为DataTable具有许多您在客户端上不一定需要的属性。您最好的选择是反序列化为List<T>并迭代该列表,根据需要添加DataTable行。