2013-10-31 79 views
0

我有folliwng腳本,實際上使兩個調用兩個不同的操作方法,並更新相關領域如何更新兩個領域: -根據返回的Json

<script type="text/javascript"> 

    $(document).ready(function() { 

     $("#Switch_RackID").change(function() { 
      var idRack = $(this).val(); 
      $.getJSON("/Switch/LoadDataCenterByRack", { id: idRack }, 
      function (RackData) { 
       var select = $("#Switch_TMSRack_DataCenter_Name"); 
       select.empty(); 
       $("#Switch_TMSRack_DataCenter_Name").val(RackData.Text); 

      }); 
      $.getJSON("/Switch/LoadZoneByRack", { id: idRack }, 
      function (RackData2) { 
       var select = $("#Switch_TMSRack_Zone_Name"); 
       select.empty(); 
       $("#Switch_TMSRack_Zone_Name").val(RackData2.Text); 

      }); 
      }); 
     }); 



</script> 

但我的問題是天氣,我可以使用getjson調用一次而不是兩次調用,然後更新相關字段?我的行動方法看起來如下: -

 public JsonResult LoadDataCenterByRack(int id) 
      { 
       string datacentername = repository.FindRack(id).DataCenter.Name; 

       var DCData = new { Text = datacentername, Value = datacentername }; 

       return Json(DCData, JsonRequestBehavior.AllowGet); 
      } 
public JsonResult LoadZoneByRack(int id) 
      { 
       string zonername = repository.FindRack(id).Zone.Name; 

       var ZData = new { Text = zonername, Value = zonername }; 

       return Json(ZData, JsonRequestBehavior.AllowGet); 
      } 

任何人都可以建議嗎? 謝謝

回答

0

當然可以。您可以使用JavaScript中console.log

console.log(response) 
返回 Anonymous object

更改您的控制器行動

public JsonResult LoadDataCenterByRack(int id) 
{ 
    string datacentername = repository.FindRack(id).DataCenter.Name; 
    var DCData = new { Text = datacentername, Value = datacentername }; 

    string zonername = repository.FindRack(id).Zone.Name; 
    var ZData = new { Text = zonername, Value = zonername }; 

    return Json(new { 
     DCData, 
     ZData 
    }, JsonRequestBehavior.AllowGet); 
} 

的JavaScript

$.getJSON("/Switch/LoadDataCenterByRack", { id: idRack }, 
function (response) { 
    $("#Switch_TMSRack_DataCenter_Name").val(response.DCData.Text); 
    $("#Switch_TMSRack_Zone_Name").val(response.ZData.Text); 
}); 

您可以檢查你得到什麼響應