2011-12-04 27 views
0

如何可以使用類似於下面的代碼東西調用的ViewResult控制器操作/方法從jquery的/ JavaScript的:如何從jquery/javascript中調用MVC3 ViewResult動作?

Index.cshtml(回調多選):

<script type="text/javascript"> 
     $("select").multiselect({ 
      click: function (event, ui) { 
       // item selected 
       // 
       // Also, not sure how I can pass in values from a multiselect to update my grid 
       // ...using ajax or post??? 
       $.post(
        '@Url.Action("Index","Main")'); 
        //, { value: ui.value, state: ui.checked ? 'checked' : 'unchecked' }); 
        // call my index() and get values or pass in values from multiselect 
       // or... 
       $.ajax(
        { url: '@Url.Action("Index","Main")'...// } 
        //, { value: ui.value, state: ui.checked ? 'checked' : 'unchecked' }); 
        // call my index() and get values or pass in values from multiselect 
           }) 
      } 
     }); 
</script> 

控制器:

// GET: /Main/ 
    public ViewResult Index(/* not sure if possible */ 
     /* get values from multiselect */ 
     string[] MultiselectId) 
    { 
     // Populate default grid view 
     IList<CModel> cs = db.CModels.OrderByDescending(x => x.CName).ToList(); 

     // get values in #multiselect and filter grid contents based on that filter value 
     // if a filter was selected... 

     return View(cs); 
    } 

回答

2
$("select").multiselect({ 
      click: function (event, ui) {     
       $.post('@Url.Action("Index","Main")',{value:ui.value},function(data){ 
       //refresh the grid here     
       },'json');    
      } 
      }); 

控制器看起來像

[HttpPost] 
public ActionResult Index(string value){ 

return Json(new{data="yourdata"}); 
} 
相關問題