2012-11-13 132 views
0

我對asp.net和MVC很新穎。mvc json請求生命週期

我正在嘗試使用json請求並填充一些文本框。

但我注意到當我使用json時,我無法訪問我視圖中其他文本框的值。 例如

string s2 = Request.Form["selectedTestCategory"]; 

將產生S2 =空,當我調試。 但如果我在頁面上提交按鈕,該值不爲空。 (到目前爲止,我知道我只能傳遞一個參數給控制器中的JSON方法)

我的問題是當我啓動一個json請求時會發生什麼?爲什麼我不能得到的Request.Form值[...]

感謝,

更新:

這是我的JSON

<script> 
$(document).ready(function() { 
    $('select#testStationUniqueId').change(function() { 
     var testStation = $(this).val(); 
     $.ajaxSetup({ cache: false }); 



     $.ajax({ 
      url: "TestInput/getTestStationInformation/" + testStation, 

      type: 'post', 
      success: function(data) { 
       $('#driveDetailDiv').empty(); 
       for (var i = 0; i < data.length; i++) { 
        $.post('TestInput/Details/', { id: data[i] }, function(data2) { 
         $('#driveDetailDiv').append(data2); 
        }); 
       } 
      } 
     }); 
    }); 
}); 

這在我的控制器中

public PartialViewResult Details(string id) 
    { 
     //DriveDetails t = new DriveDetails(id); 
     //return PartialView("DriveDetailsPartial", t); 

     test_instance_input_model ti = new test_instance_input_model(); 
     string s2 = Request.Form["selectedTestCategory"]; 
     repository.setTestInstanceAttributes(ti, id); 

     return PartialView("TestInstancePartial", ti); 
    } 

s2在Details中爲null,但是如果我使用提交按鈕,它將具有正確的值。

所以我想弄清楚爲什麼它是空的,當我發送一個JSON請求。

+0

發佈ajax代碼和控制器齒列。還要指定您正在使用的MVC版本。 – asawyer

+0

對不起,行動的*定義*不是它的牙科記錄。 – asawyer

+0

謝謝,我把代碼放在我的問題 – Athena

回答

1

在JavaScript中,您不包括jQuery ajax請求中的任何數據(請參閱jQuery ajax)。因此jQuery沒有添加任何請求參數。你需要包含一個jQuery將變成參數的數據對象,即數據中的更多屬性對象請求中的參數越多。

$.ajax({ 
    url: '', 
    data: { selectedTestCategory: 'category' }, 
    dataType: 'post', 
    success: function() {} 
}); 

此外,在您的控制器中,您可以快捷方式到請求參數。

string s2 = Request["selectedTestCategory"]; 
+0

謝謝WCM! :d – Athena