2017-05-07 56 views
0

我在asp.net mvc的编写web服务,仍然有下面的代码连接相同的错误:编辑视图在asp.net mvc的 - 控制器错误

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编辑(Int32)已在类型 WebApplication2。 Controllers.DefaultController System.Web.Mvc.ActionResult编辑在 型WebApplication2.Controllers.DefaultController(WebApplication2.Models.Customer)

任何人可以帮助我解决吗?

回答

2

这是因为你用错误的HttpPost属性修饰了你的控制器动作。您需要this one

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

你已经使用了一个(System.Web.Http.HttpPost)时,放置在ASP.NET MVC控制器动作是专门为的ASP.NET Web API操作,并具有严格的零影响。因此,ASP.NET MVC框架无法消除2 Edit操作之间的歧义,因为它们使用的是相同的动词,因此您会收到错误。

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