0

I have two methods:

public ActionResult Index(int? id)
{
    return Redirect("/View/" + this.GetMenuItems().First().Id);
}

public ActionResult Index(int id, uint? limit)
{

when i go to /View/1 - i get that error

he current request for action 'Index' on controller type 'ViewController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index(System.Nullable1[System.Int32]) on type SVNViewer.Controllers.ViewController System.Web.Mvc.ActionResult Index(Int32, System.Nullable1[System.UInt32]) on type SVNViewer.Controllers.ViewController

I need that two methods but remove Ambiguous error, how can i do that?

testCoder
  • 7,155
  • 13
  • 56
  • 75
  • 1
    Rename one of the methods to something else: The second method to Index -> MenuItem perhaps? Or, make the limit parameter required (not nullable) in the second. – Tommy Oct 11 '12 at 14:08
  • @Tommy I would have given a vote if you replied that as an answer – VJAI Oct 11 '12 at 14:54
  • @Mark - I appreciate that, but I didn't have enough time to formulate a good answer when I came across this, but wanted to throw in my 2 cents for testCoder! – Tommy Oct 11 '12 at 15:42

3 Answers3

2

Change your second Action to not have an nullable uint.

public ActionResult Index(int id, uint limit)

Your Index(int? id) should be the method which handles if limit is null.

Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
2

You can use ActionName to work around this: Purpose of ActionName

If both do the same thing, you can do instead:

public ActionResult Index(int? id, uint? limit = null)
{
  ...
}

Make the second parameter optional.

Or have one have [HttpGet] attribute and one have [HttpPost], if one responds to a get and the other responds to a posted form.

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
1

You could create a model and have only one action method:

public class MyActionModel 
{
    public int? Id { get;set; }
    public int? Limit { get;set; }
}

Then in your controller:

public ActionResult Index(MyActionModel model)
{
    // Your code here
    if (model.Id.HasValue() { // do something....etc}
}
kevin_fitz
  • 837
  • 6
  • 19