2016-12-22 10 views
1

Googleで検索してドキュメントを読んだ後、これがどのように動作するのか混乱しています。値を持つジェネリックハンドラを呼び出し、成功すると複数の値を返すとき。データベースのレコードをどのようにループしますか?ジェネリックハンドラのレコードをループするか、JQueryの各関数を使用して成功するか?以下は、複数の値が存在する場合に動作しない現在のコードです。の$ .each(VARDATA、機能を()を削除し、それが動作します。ただし、1つのレコードのみが表示されJQueryジェネリックハンドラから複数の値を取得する

これは、データを使用すると、テキストボックスにエリーに入力した後に見て、ボタン

をクリックする方法です。
Business Profile ID: 8 
Business Name: The Boston Store 
Phone Number: 814-455-1478 
E-Mail: [email protected] 

Business Profile ID: 9 
Business Name: Sam The Man Pizza 
Phone Number: 814-868-3809 
E-Mail: [email protected] 

jQueryのスクリプト

$(document).ready(function() { 
     $('#button').click(function() { 
      $.ajax({ 
       contentType: "text/html; charset=utf-8", 
       data: "ID=" + $('#businessSelect').val(), 
       url: "getTest.ashx", 
       dataType: "text", 
       success: function (data) { 
        var vardata = JSON.parse(data); 
        $.each(vardata, function (index, value) { 
         $("#BusProfileID").html(value.BusProfileID); 
         $("#BusinessName").html(value.BusinessName); 
         $("#BusinessPhone").html(value.BusinessPhone); 
         $("#BusinessEmail").html(value.BusinessEmail); 
        }); 
       } 
      }) 
     }); 
    }); 

ASHXハンドラページ

 public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "text/html"; 
     string ID = context.Request.QueryString["ID"]; 
     SqlConnection conn; 
     SqlCommand comm; 
     SqlDataReader reader; 
     string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 
     conn = new SqlConnection(connectionString); 
     comm = new SqlCommand("SELECT BusProfileID, BusinessName, BusinessPhone, BusinessEmail FROM [BusProfile] WHERE BusinessCity = @BusinessCity", conn); 
     comm.Parameters.Add("@BusinessCity", System.Data.SqlDbType.VarChar); 
     comm.Parameters["@BusinessCity"].Value = ID; 
     try 
     { 
      conn.Open(); 
      reader = comm.ExecuteReader(); 
      List<BusinessData> objList = new List<BusinessData>(); 
      BusinessData objData; 
      while (reader.Read()) 
      { 
       objData = new BusinessData(); 
       objData.BusProfileID = reader["BusProfileID"].ToString(); 
       objData.BusinessName = reader["BusinessName"].ToString(); 
       objData.BusinessPhone = reader["BusinessPhone"].ToString(); 
       objData.BusinessEmail = reader["BusinessEmail"].ToString(); 
       objList.Add(objData); 
       context.Response.Write(JsonConvert.SerializeObject(objList)); 
      } 
      reader.Close(); 
     } 

     finally 
     { 
      conn.Close(); 
     } 
    } 

    public class BusinessData 
    { 
     public string BusProfileID { get; set; } 
     public string BusinessName { get; set; } 
     public string BusinessPhone { get; set; } 
     public string BusinessEmail { get; set; } 
    } 
    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

ASPXページ

  <div class="row"> 
       <div class="columns medium-12"> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Select City:</label> 
          <input type="text" id="businessSelect" style="height:2em;" /> <input type="button" id="button" value="Click me" /> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Business Profile ID:</label> 
          <label id="BusProfileID" style="font-size:1em;"></label> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Business Name:</label> 
          <label id="BusinessName" style="font-size:1em;"></label> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Phone Number:</label> 
          <label id="BusinessPhone" style="font-size:1em;"></label> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label id="BusinessEmail" style="font-size:1em">E-Mail:</label> 
         </div> 
        </div> 
       </div> 

答えて

0

あなたは結果を通じてloopにしたい場合は、それがobjectでなければなりません。今あなたはstringifyあなたの結果です。あなたはそれを反復することはできません。

使用JSON.parseですから、分割しarrayに行う必要はありません

var vardata = JSON.parse(data); 
$.each(vardata, function (index, value) { 
    // TODO 
}); 

そのオブジェクトに$.eachを使用し、その後JSONオブジェクトに結果を変換します。 each文でvalueパラメータを参照できます。 JSONからの値を使用するには

EDIT

、サーバーの結果から、セットとして、そのプロパティを使用することができます。

と同様に、

$.each(vardata, function (index, value) { 
    $("#BusProfileID").html(value.BusProfileID); 
    $("#BusinessName").html(value.BusinessName); 
    $("#BusinessPhone").html(value.BusinessPhone); 
    $("#BusinessEmail").html(value.BusinessEmail); 
}); 

List<BusinessData>オブジェクトを作成し、このリストに読者から

​​ を各項目を追加 SQL Query

public class BusinessData 
{ 
    public long BusProfileID { get; set; } 
    public string BusProfileID { get; set; } 
    public string BusProfileID { get; set; } 
    public string BusProfileID { get; set; } 
} 

からあなたが取得したデータを保持するためにclassを使用して、

JsonConvertが含まれていますNewtonsoft.Jsonナゲットパッケージにあります。

+0

結果は返されません。元の質問を編集してあなたの提案を示しました。 –

+0

あなたの追加提案に基づいてまだ結果が表示されていません。しかし、この問題を解決するために、JSONの仕組みを理解しています。 –

関連する問題