如何从jquery / javascript调用MVC3 ViewResult操作?

时间:2011-12-04 20:05:01

标签: asp.net asp.net-mvc-3 jquery

如何使用类似于以下代码的内容从jquery / javascript调用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);
    }

1 个答案:

答案 0 :(得分: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"});
 }