2011-02-13 69 views
1

我正在嘗試使用jQuery Grid插件與ASP.NET MVC來顯示數據。這似乎提供了在網絡上的實例表明,該控制器動作簽名如下所示:從jQuery Grid插件向ASP.NET MVC控制器動作傳遞額外參數

public ActionResult SomeMethod(string sidx, string sord, int page, int rows) { } 

有什麼辦法,我可以通過從頁面上的各種輸入字段額外參數?

乾杯, D.

補充:我還沒有編寫任何代碼,但尚未在客戶端代碼將是這樣的:

jQuery(document).ready(function(){ 
     jQuery("#list").jqGrid({ 
     url:'/Entry/GridData/', 
     datatype: 'json', 
     mtype: 'POST', 
     colNames:['Entry Date','Registration','Registered Name'], 
     colModel :[ 
      {name:'EntryDate', index:'EntryDate', width:40 }, 
      {name:'Registration', index:'Registration', width:200 }, 
      {name:'RegisteredName', index:'RegisteredName', width:200 }], 
     pager: jQuery('#pager'), 
     rowNum:20, 
     rowList:[5,10,20,50], 
     altRows: true, 
     sortable: false, 
     viewrecords: true, 
     caption: 'My first grid' 
     }); 
    }); 

而服務器端代碼是這樣的:

public ActionResult GridData(string sidx, string sord, int page, int rows) 
{ 
    //Some stuff to get data, which needs a couple of extra parameters on top of the ones in the signature 
    return Json(dataCollection); 
} 

在GridData操作中,我需要更多的參數來獲取適當的數據。如何在JavaScript中指定這些內容,以及如何在控制器操作中獲取它們?

回答

8

您可以使用postData屬性:

$('#list').jqGrid({ 
    url: '@Url.Action("SomeMethod")', 
    datatype: 'json', 
    mtype: 'POST' 
    postData: { 
     someParam1: $('#someInput1').val(), 
     someParam2: $('#someInput2').val() 
    } 
    ... 
}); 

,並在你的控制器動作,您可以添加此參數:

public ActionResult SomeMethod(string someParam1, string someParam2, string sidx, ...) { } 
+0

太棒了!在發佈問題更新後不久,我看到了postData:屬性。它看起來很有希望,但我猜mtype:屬性必須設置爲'POST'?或者這也適用於GET? – codedog

+0

@DanyW,我認爲它也可以和GET一起工作,但不是100%肯定的,還沒有測試過。如果沒有,你可以追加到URL:'url:'@ Url.Action(「SomeMethod」)?' + $ .param({someparam:'some value'})' –

+0

工作就像一個魅力!它也適用於GET,只要返回JsonRequestBehavior.AllowGet指定!我不需要將其附加到網址。再次感謝! – codedog

1

創建與特定輸入的目的相對應的另一個操作方法,並接受您想要接受的參數。如果你需要在處理其他的結果結束,回到這個動作的結果,請致電RedirectToRoute

+0

對不起,我不完全確定它是如何工作的。如何在jQuery中包含額外的數據(「#list」)?jqGrid({...});呼叫?我如何在控制器操作中選擇它?謝謝。 – codedog

+0

我不得不看更多的來源。 – smartcaveman

相關問題