2

I am attempting to make a simple test website to allow me to list, create, edit and delete customer objects using MVC4.

Inside my controller I have 2 create methods, a Get for when the form loads with the controls, and a Post that actually saves the data.

    //
    // GET: /Customer/Create

    [HttpGet]
    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /Customer/Create

    [HttpPost]
    public ActionResult Create(Customer cust)
    {
        if (ModelState.IsValid)
        {
            _repository.Add(cust);
            return RedirectToAction("GetAllCustomers");
        }

        return View(cust);
    }

However when I run the project and attempt to access the create action I get an error that:

The current request for action 'Create' on controller type 'CustomerController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type [Project].Controllers.CustomerController
System.Web.Mvc.ActionResult Create([Project].Models.Customer) on type [Project].Controllers.CustomerController

My I understand that it can't see the difference between my Get and Post methods, but I have added the attribues. What could be the cause of this and how can I make it work again?

tereško
  • 58,060
  • 25
  • 98
  • 150
user1755802
  • 81
  • 1
  • 6
  • try clean and rebuild the project. – Vijayant Katyal Oct 18 '12 at 09:58
  • you need to either add attribute for overload or attribute for acceptverbs see this http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Zaki Oct 18 '12 at 09:59
  • this code looks fine - are you sure you get that error after adding the http attributes? – Mike Simmons Oct 18 '12 at 10:08
  • Yes, I still get the error after adding the HTTP attribues. I have also cleaned and rebuilt the project as @VijayantKatyal suggested – user1755802 Oct 18 '12 at 10:10
  • 1
    Could you elaborate on how you "attempt to access the create action"? – Yakimych Oct 18 '12 at 10:13
  • You may add the wrong Web APi DLL, pls show `using` – cuongle Oct 18 '12 at 10:14
  • @Yakimych When I say "access the create action" I mean by trying to access the page in my browser http://localhost:64349/Customer/Create – user1755802 Oct 18 '12 at 10:26
  • Try renaming the POST action method name to see if it resolves your issue - if it doesn't then you're not running the latest code on your web server – Mike Simmons Oct 18 '12 at 10:27
  • @MikeSimmons Yeah If I rename the post action to CreatePost then the GET Create action loads correctly – user1755802 Oct 18 '12 at 10:30
  • and rename it back starts failing again? – Mike Simmons Oct 18 '12 at 10:32
  • 1
    if it does, can you check what assembly the HttpGetAttribute and HttpPostAttribute are referenced from? Should be System.Web.Mvc v4 – Mike Simmons Oct 18 '12 at 10:37
  • @CuongLe You are correct, thank you sir. I had "using System.Web.Mvc;" sitting out side of my namespace, and "using System.Web.Http;" sitting inside my namespace. This conflict meant that the HTTP attribues were bein used, not the MVC ones. Thanks to Mike Simmons too. – user1755802 Oct 18 '12 at 10:39

1 Answers1

2

MVC does not authorize you to have 2 action methods with the same name.

BUT you can have 2 action methods with the same URI when the http verb differs (GET, POST). Use the ActionName attribute to set the action name. Don't use the same methods names. You can use any name. A convention is to add the http verb as the method suffix.

[HttpPost]
[ActionName("Create")]
public ActionResult CreatePost(Customer cust)
{
    if (ModelState.IsValid)
    {
        _repository.Add(cust);
        return RedirectToAction("GetAllCustomers");
    }

    return View(cust);
}
Softlion
  • 12,281
  • 11
  • 58
  • 88