2012-12-07 41 views
0

我可以使用jQuery的Ajax請求來過濾webgrid,我有幾個webgrids和幾個ajax請求。每個Ajax請求都調用相同的ActionResult方法,但傳遞不同的參數。這個想法是基於我提供的參數調用的正確方法。但是,這不。JQuery的Ajax請求已超載的ActionResult方法

我得到這個錯誤:

The current request for action 'FilterGrid' on controller type 'UserManagerController' is ambiguous between the following action methods:<br>System.Web.Mvc.ActionResult FilterGrid(Int32, Int32, System.String, System.String, Int32, System.String) on type UserManager.Controllers.UserManagerController<br>System.Web.Mvc.ActionResult FilterGrid(System.String) on type UserManager.Controllers.UserManagerController 

我的jQuery

function filterGrid() { 
     var filters = getFilterVals(); 
     $.ajax({ 
      url: '@Url.Action("FilterGrid", "UserManager")', 
      type: "POST", 
      async: true, 
      dataType: "html", 
      data: "group=" + filters.group, 
      success: function (data) { 
       $('#webgrid-wrapper').empty().html(data); 
      }, 
      error: function (data) { 

      } 
     }); 
    } 

我的控制器代碼

[HttpPost] 
public ActionResult FilterGrid(int alf, int brad, string username, string group, int isactive, string email) 
{ 

    List<UserManager.Models.vw_UserManager_Model> modelList = DAL.getGridUsers(alf, brad, username, group, isactive, email); 

    switch (alf) 
    { 
     case 1: 
      Session["chkBoxAlf"] = 1; 
      break; 
     case 0: 
      Session["chkBoxAlf"] = 0; 
      break; 
    } 

    switch (brad) 
    { 
     case 1: 
      Session["chkBoxBrad"] = 1; 
      break; 
     case 0: 
      Session["chkBoxBrad"] = 0; 
      break; 
    } 

    switch (isactive) 
    { 
     case 1: 
      Session["chkBoxIsActive"] = 1; 
      break; 
     case 0: 
      Session["chkBoxIsActive"] = 0; 
      break; 
    } 

    Session["txtFilterUsername"] = username; 
    Session["txtFilterGroup"] = group; 
    Session["txtFilterEmail"] = email; 

    return PartialView("~/Views/Partial/_WebGridUserManagerUsers.cshtml", modelList); 
} 

[HttpPost] 
public ActionResult FilterGrid(string group) 
{ 
    return PartialView("~/Views/Partial/_WebGridUserManagerGroups.cshtml"); 
} 

我breakpo int for

[HttpPost] 
     public ActionResult FilterGrid(string group) 
     { 
      return PartialView("~/Views/Partial/_WebGridUserManagerGroups.cshtml"); 
     } 

沒有被擊中。有誰知道如何解決這個問題?

回答

4

你不能有兩個名稱相同的方法POST,因爲該框架不知道哪個叫。 但是可以配置ActionName:

[ActionName("MethodName")] 
public ActionResult Filter(string group){} 

,並在你的Ajax調用〜/控制器/方法名

+0

感謝這工作。除非有更好的方法,否則我會使用它。 –

0

兩個函數具有相同的名稱,嘗試重命名其中之一..

+0

這是整點。我使用方法重載,所以我使用相同的Ajax請求,但提供不同的參數,所以代碼知道使用正確的方法。 –