2016-10-11 71 views
-1

我有一個問題,我一直堅持了一段時間。如何將參數發送到mvc控制器中的jsonresult操作?

我有一個JsonResult方法在我的控制器(SubSubCategoriesController): -

public JsonResult GetSubCategories(int CategoryID) 
    { 
     return Json((db.SubCategories.Select(p => new { CategoryID = p.CategoryID, SubCategoryID = p.SubCatgeoryID, SubCategoryName = p.SubCategoryName })).Where(p => p.CategoryID == CategoryID), JsonRequestBehavior.AllowGet); 
    } 

現在,我想從我的查看發送參數(類別ID)的值?我怎樣才能做到這一點? 在此先感謝

+1

顯示您的視圖代碼,調用此方法 –

+0

@StephenMuecke我沒有嘗試一些'HTML助手「,但沒有成功 – Medo

+0

@Medo你需要通過javascript ajax調用。你寫了這個嗎?如果是這樣,請顯示它。 – ADyson

回答

0

如果您的數據不是viewmodel結構,我的意思是如果你只是有興趣發送一個單一的參數,那麼你可以使用下面的ajax函數定義它從視圖。

$.ajax({ 
     type: 'GET', 
     url: '<%= Url.Action("GetSubCategories") %>', 
     cache: false, 
     data: { CategoryID : <bind it from UI> }, 
     success: function (data) { 
      console.log(data); 
     }, 
     error: function (xhr) { 
      alert('Error: ' + xhr.statusText);  
     } 
}); 

否則另一個整潔的方式

$.post("controller/method", {CategoryID: "<From UI>"}, function(result) { 
    console.log(result) 
}); 

而只是裝點處理法httpPost

[httpPost] 
public JsonResult GetSubCategories(int CategoryID) 
{ 
} 

$.post here

相關問題