【问题标题】:Edit View in asp.net mvc - controller error在 asp.net mvc 中编辑视图 - 控制器错误
【发布时间】:2017-05-07 15:17:53
【问题描述】:

我正在用 asp.net mvc 编写 Web 服务,但与下面的代码相关联时仍然出现相同的错误:

    public ActionResult Edit(int C)
    {
        var customers = _ourCustomerRespository.GetCustomers();
        var std = customers.Where(s => s.CustomerID == C).FirstOrDefault();

        return View(std);
    }

    [System.Web.Http.HttpPost]
    public ActionResult Edit(Customer C)
    {
        return RedirectToAction("Index");
    }

当我尝试使用我的服务时出现错误消息

当前对控制器类型的“编辑”操作请求 'DefaultController' 在以下操作方法之间不明确: System.Web.Mvc.ActionResult Edit(Int32) 类型 WebApplication2.Controllers.DefaultController System.Web.Mvc.ActionResult Edit(WebApplication2.Models.Customer) on 键入 WebApplication2.Controllers.DefaultController

谁能帮我解决这个问题?

【问题讨论】:

    标签: c# asp.net-web-api


    【解决方案1】:

    那是因为您使用 错误 HttpPost 属性修饰了控制器操作。你需要this one:

    [System.Web.Mvc.HttpPost]
    public ActionResult Edit(Customer C)
    

    您使用的那个 (System.Web.Http.HttpPost) 是为 ASP.NET Web API 操作而设计的,当放置在 ASP.NET MVC 控制器操作上时,效果严格为零。因此 ASP.NET MVC 框架无法消除 2 个 Edit 动作之间的歧义,因为它们使用相同的动词,因此您会收到错误消息。

    备注:请确保您正确标记您的问题,以避免任何混淆。目前您使用的是asp.net-web-api,而您显示的控制器操作显然是asp.net-mvc。这是两个完全不同的框架。

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      相关资源
      最近更新 更多