asp.net-mvc
2011-03-04 76 views 0 likes 
0

我有以下內容來獲取從控制器傳遞的Json abject,並在視圖中填充各種文本框。但是,即使控制器正在傳遞有效的Json對象,也沒有任何事情發生。這段代碼有什麼問題?使用JSon對象將數據從控制器傳遞到視圖

<script language="javascript" type="text/javascript"> 

$(document).ready(function() { 
    var url = '<%=Url.Action("DropDownChange") %>'; 
    $("#vendorID").change(function() { 
     var selectedID = $(this).val(); 
     if (selectedID != "New Vendor Id") { 
      //$.post('Url.Action("DropDownChange","Refunds")', function(result) { 
      $.post(url, { dropdownValue: selectedID }, function(result) { 
       alert(selectedID); 
       $("#name").val(result.Name); 
       $("#city").val(result.City); 
       $("#contact").val(result.Contact); 
       $("#address2").val(result.Address2); 
       $("#address1").val(result.Address1); 
       $("#state").val(result.State); 
       $("#zip").val(result.Zip); 

      }); 

     } 
    }); 

}); 

這是在我的控制器的代碼;

public JsonResult DropDownChange(string dropdownValue) 
    // This action method gets called via an ajax request 
    { 


     if (dropdownValue != null && Request.IsAjaxRequest() == true) 
     { 

      paymentApplicationRefund = 
      cPaymentRepository.PayableEntity(dropdownValue); 

      paymentApplicationRefund.Address1.Trim(); 
      paymentApplicationRefund.Address2.Trim(); 
      paymentApplicationRefund.Name.Trim(); 
      paymentApplicationRefund.City.Trim(); 
      paymentApplicationRefund.Contact.Trim(); 
      paymentApplicationRefund.State.Trim(); 
      paymentApplicationRefund.Zip.Trim(); 

      return Json(paymentApplicationRefund,"application/json");    
     } 

     else 
     { 
      return null; 
     } 
    } 
+0

任何javascript錯誤? – 2011-03-04 20:51:08

+0

我看不到任何錯誤 - 我正在使用IE瀏覽器的螢火蟲。 – JamaicasFinest 2011-03-04 20:54:15

回答

0

您可能只需要告訴它期望返回JSON數據。默認情況下,它假設它是HTML。

$.post(url, { dropdownValue: selectedID }, function(result) { 
       alert(selectedID); 
       $("#name").val(result.Name); 
       $("#city").val(result.City); 
       $("#contact").val(result.Contact); 
       $("#address2").val(result.Address2); 
       $("#address1").val(result.Address1); 
       $("#state").val(result.State); 
       $("#zip").val(result.Zip); 
      }, 'json'); 
+0

嘗試過,但仍然沒有... – JamaicasFinest 2011-03-04 21:07:46

+0

基於我在這裏 - 是否有任何其他可能的方法,我可以採取? – JamaicasFinest 2011-03-04 22:02:09

+0

@Jam - 您是否使用Firebug查看了哪些數據實際發送了?並確保提出請求? – tvanfosson 2011-03-04 22:35:22

0

我喜歡與我的DTO發送的Json到的ActionResult作爲參數,並使用JsonValueProviderFactory爲我做反序列化。

Sending JSON to an ASP.NET MVC Action Method Argument

0

嘗試...... 添加 「.change()」 在函數結束。

$(document).ready(function() {  
    var url = '<%=Url.Action("DropDownChange") %>'; 
    $("#vendorID").change(function() {   
    ..... 
    }).change(); 
}); 
相關問題