2013-03-27 34 views
1

我有這樣的:獲取JSON結果列表到現有的HTML選擇?

@Html.DropDownList("centres", (List<SelectListItem>)ViewData["centres"]) 

我需要的程序運行時,使用JQuery與訂立SQL數據庫中的數據進行更新:

jQuery(function ($) { 
    jQuery("body").on('click', "#create", function() { 
     $.getJSON("/Centres/updateList", function (results1) { 
// perform update here using results1 from jsonresult updateList in Controller class 
     }); 
    }); 
}); 

的結果:1是從列表中我MVC控制器類。我希望能夠使用這些新結果編輯現有的html下拉列表。目前它顯示了一個selectListItems的列表,但是在控制器類的updateList中,我創建了一個新的列表,它基於我剛創建的一個新的中心。我的控制器類方法如下所示:

public JsonResult updateList() 
{ 
    List<SelectListItem> centres = new List<SelectListItem>(); 
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Michael\Documents\Visual Studio 2012\Projects\OneEightyWebApp\OneEightyWebApp\App_Data\OneHoldings.mdf;Integrated Security=True"); 
    SqlCommand cmd = new SqlCommand("SELECT Centre.Centre_Name, Count(Shop_No) AS Shop_Count FROM Centre LEFT JOIN Space ON Centre.Centre_Name = Space.Centre_Name GROUP BY Centre.Centre_Name;", conn); 
    DataTable dt = new DataTable(); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    conn.Open(); 
    da.Fill(dt); 
    conn.Close(); 
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     centres.Add(new SelectListItem { Text = dt.Rows[i].ItemArray.GetValue(0).ToString(), Value = dt.Rows[i].ItemArray.GetValue(1).ToString() }); 
    } 
    return Json(centres, JsonRequestBehavior.AllowGet); 
} 

任何想法?

+0

如果我們想你從服務器得到什麼預先設定的選擇? – undefined 2013-03-27 22:33:02

+0

這是一個列表。在updateList中創建的中心名稱列表。我創建了更多中心,然後需要使用新中心更新此