2013-08-02 25 views
0

服務器端功能,我想要做的步驟如下:通行證JavaScript數組與jQuery的

  1. 取複選框值的列表和使它們成爲在JavaScript字符串數組。
  2. 取出數組並將其發送到服務器端函數。
  3. 取數組服務器端並將其放入DataTable中。
  4. 取出DataTable並將其作爲TVP發送到存儲過程。

我已經從服務器端開始工作了。我遇到麻煩的地方是從JavaScript到服務器。

隨着我當前的代碼,我得到這個錯誤:

There are not enough fields in the Structured type. Structured types must have at least one field.

我如何通過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; 
      } 
     } 

} 

如果這是一個完整的愚蠢辦法做到這一點,修改非常歡迎:)

回答

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

1

這裏的問題是,你有兩種方法在你的web表單上需要一個回發週期。

  • 當你調用WebMethod,它會創建_default類的一個實例,並設置dt變量,但也無可奈何它。
  • 當您撥打Button1_Click時,會創建一個_default類的新實例,因此dt變量設置爲new DataTable();。因此,您的參數沒有列或其他數據。

你會想要在一種方法中處理DataTable,推測是TableFilter方法。