2010-05-10 73 views
0

我正在使用MVC和jQuery,我試圖顯示某人的個人資料與該人屬於一些額外的機構。我是新來這一點,但我做了這樣的事情在ProfileControler:MVC和jQuery數據檢索

public ActionResult Institutions(int id) 
    { 
     var inst = fr.getInstitutions(id); 
     return Json(inst); 
    } 

getInstitutions(id)返回機構對象(含名稱,城市,郵編等) 然後在某個視圖我想使用jQuery獲取數據並將其顯示如下:

$(document).ready(function() { 
     $.post("/Profile/Institutions", { id: <%= Model.Profile.userProfileID %> }, function (data) { 
      $.each(data, function() { 

       var new_div = $("<div>"); 

       var new_label = $("<label>"); 
       new_label.html(this.City); 

       var new_input_b = $("<input>"); 
       new_input_b.attr("type", "button"); 

       new_div.append(new_label); 
       new_div.append(new_input_b); 

       $("#institutions").append(new_div); 
      }); 
     }); 
    }); 

$(「#institutions」)是一個div,我想要顯示所有結果。 .post工作正確無誤,因爲某些機構從數據庫中檢索並作爲Json結果傳遞給視圖。但是,我擔心它不會與每個.each進行交互。

任何幫助,評析或在某些方向指向將大大appriciated

回答

1

上面的代碼將工作,如果你set the dataType to JSON

$(document).ready(function() { 
     $.post("/Profile/Institutions", 
     { id: <%= Model.Profile.userProfileID %> }, 
     function (data) { 
      $.each(data, function() { 

       var new_div = $("<div>"); 

       var new_label = $("<label>"); 
       new_label.html(this.City); 

       var new_input_b = $("<input>"); 
       new_input_b.attr("type", "button"); 

       new_div.append(new_label); 
       new_div.append(new_input_b); 

       $("#institutions").append(new_div); 
      }); 
     }, 
     "json"); 
    }); 
+0

+1打我吧 – munch 2010-05-10 18:13:45