【问题标题】:The current request for action 'AdminManagement' on controller type 'AdminController' is ambiguous between the following action methods当前对控制器类型“AdminController”的操作“AdminManagement”的请求在以下操作方法之间不明确
【发布时间】:2017-10-25 18:10:51
【问题描述】:
[Route("admins")]
public ActionResult AdminManagement()
{
    EmployeeBuslayer lstEmp = new EmployeeBuslayer();
    AdminManamegemtModel comModel = new AdminManamegemtModel();
    comModel = lstEmp.GetAdminsWithRole;
    return View(comModel);
}

#region AdminMaster
[HttpPost]
[ActionName("AdminManagement")]
public ActionResult AdminManagementPost()
{
    string SearchText = Request.Form["Search"].ToString().ToLower();
    EmployeeBuslayer lstEmp = new EmployeeBuslayer();
    List<AdminMaster> dataEmployee = null;
    AdminManamegemtModel comModel = new AdminManamegemtModel();
    AdminManamegemtModel comModelfromDB = lstEmp.GetAdminsWithRole;
    dataEmployee = comModelfromDB.AdminMasterModel.Where(src => src.UserName.ToLower().Contains(SearchText) || src.EmailID.ToLower().Contains(SearchText)).ToList();
    comModel.AdminMasterModel = dataEmployee;
    comModel.AdminRoleMasterModel = comModelfromDB.AdminRoleMasterModel;
    return View("AdminManagement", comModel);

}

以上两种方法用于相同的视图。我有搜索按钮。当点击搜索它给我下面的错误。

当前对控制器类型的“AdminManagement”操作请求 'AdminController' 在以下操作方法之间不明确: System.Web.Mvc.ActionResult AdminManagementPost() 类型 MVCAdminDemo.Controllers.AdminController System.Web.Mvc.ActionResult MVCAdminDemo.Controllers.AdminController 类型上的 AdminManagement()。一世 可以通过从 AdminManagementPost() 中删除 ActionName 来解决此错误 ,但问题是 URL 更改,我不希望那样。网址应该是 http://localhost:52499/admin/admins

AdminManagement() 是 Get 方法在加载时加载的默认值

【问题讨论】:

    标签: c# url model-view-controller routing attributerouting


    【解决方案1】:

    我认为动作名称在这里无关紧要。由于您没有为第一个操作方法指定任何 http 动词,因此它被视为 post。因此,每当您尝试调用此路由时,它都是模棱两可的,因为两个带有动词 post 的操作方法在同一路由上可用。用 [HttpGet] 装饰第一个操作方法,因为它看起来像一个 get 操作。示例代码如下:

        [Route("api/datasync/get")]
        [HttpGet]
        public IHttpActionResult GetOne()
        {
            return Ok(1);
        }
    
        [Route("api/datasync/get")]
        [HttpPost]
        public IHttpActionResult GetOnes()
        {
            return Ok(1);
        }
    

    【讨论】:

    • [Route("admins")] [HttpGet] public ActionResult AdminManagement() { //code } [Route("admins")] [HttpPost] public ActionResult AdminManagementPost() { //code }不幸的是没有加载它工作但是当点击搜索帖子方法时,它显示没有找到资源。我猜我使用了错误的方法名称? @using (Html.BeginForm("AdminManagement", "Admin")) { //html 和按钮 } jsfiddle.net/93t5xfg7
    • 操作名称是 AdminManagementPost 对吗?尝试将其从 Html.BeginForm 中的 AdminManagement 更改为 AdminManagementPost,类似于 Html.BeginForm("AdminManagementPost ", "Admin", FormMethod.Post, null)
    • 不,它不工作。好吧,谢谢我试试看。感谢帮助 。如果你有任何东西,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多