The current request for action 'Index' on controller type 'DinnersController' is ambiguous between the following action methods [closed]
Asked Answered
T

2

8

I am getting this error after trying to implement paging support.

I am on this step of the html tutorial: http://nerddinnerbook.s3.amazonaws.com/Part8.htm

Tautologism answered 27/9, 2011 at 19:53 Comment(1)
Could you provide the actual error output and stack trace? Otherwise it is hard to tell where you are and what it is that you are doing when the application throws the error.Cenobite
F
23

The error you are getting tells that ASP.NET MVC has found two actions with the same name and can't chose which to use.

If you have two Index-actions in your DinnersController.cs:

public ActionResult Index() {

and:

public ActionResult Index(int? page) {

Then you should remove the first Action. since the second is the 'updated' version of the first. The second Action accepts requests to:

/Dinners

/Dinners?page=2

/Dinners/Index

/Dinners/Index?page=2

And with the change in RegisterRoutes it also accepts requests to:

/Dinners/Page/2

You can have two Actions with the same name, providing one is for saving (postbacks), where you decorate the saving action with [AcceptVerbs(HttpVerbs.Post)] or just [HttpPost]

Frye answered 27/9, 2011 at 20:39 Comment(1)
IMHO people do this "2 get methods, same name, different signature" because of the absolute ton of blogs, books and docs that introduce the idea of UrlParameter.Optional as a way to 'save having extra routes". So for the classic "public ActionResult List()" route matching they'll say add the UrlParameter enum for a route as in: routes.MapRoute("simple", "{controller}/{action}/{id}", new {id = UrlParameter.Optional}); Then they'll show you a "public ActionResult List(int? id)". What they almost never state is that you have to remove the original "ActionResult List()" method for this to work.Aurochs
T
21

MVC does not support overloading of action methods in your controller. It is not possible to have two Index() actions with different method signatures on the same controller.

In order to make the example you are testing work, you must remove the Index() action which does not accept any parameters, and replace it with the action that takes a nullable int as a parameter.

Thunderbolt answered 27/9, 2011 at 19:58 Comment(1)
it would be nice if MVC supported overloading of actions. I would like to be able to swap between multiple version of my action, let's say by having a [PriorityAttribute(10)] so that this action is taken first. Then I can just change that number from a database configuration or as I want.Shakhty

© 2022 - 2024 — McMap. All rights reserved.