4

Possible Duplicate:
Can you overload controller methods in ASP.Net MVC?

I need to 2 methods that takes different type of argument. so I tried this,

[HttpPost]
public ActionResult ItemUpdate(ORDER ln)
{
   // do something
}

[HttpPost]
public ActionResult ItemUpdate(List<ORDER> lns)
{
  // Do Something
}

but it does not work.

No error while compiling, but when run, it makes an error.

How I write the code to make that works?

Thanks!

[Edit]

[HttpGet]
public ActionResult ItemUpdate(string name)
{
    return Content(name);
}

[HttpGet]
public ActionResult ItemUpdate(int num)
{
    return Content(num.ToString());
}

and when I call /Test/ItemUpdate/

it make an error,

Server Error in '/' Application.
The current request for action 'ItemUpdate' on controller type 'OrderController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult ItemUpdate(System.String) on type Ecom.WebUI.Controllers.OrderController
System.Web.Mvc.ActionResult ItemUpdate(Int32) on type Ecom.WebUI.Controllers.OrderController 

[EDIT]

It does not match with ORDER even single parameter.

if (lns.GetType() == typeof(ORDER))
{
  // always false
}else{
  // also I can not cast the object.
  ORDER ln = (ORDER)lns; //Unable to cast object of type 'System.Object' to type 'ORDER'
}
Community
  • 1
  • 1
Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • 1
    what error are you getting? what are you trying to accomplish? – Forte L. Jul 18 '12 at 18:49
  • I think you're on the right path. It is acceptable to overload C# methods with different signatures like this, different parameter types. Does your compiler recognize the ORDER object? Is it balking because the methods don't return anything (you can return null for the moment)? – DOK Jul 18 '12 at 18:52
  • 3
    The issue is that this is MVC, see: http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – CaffGeek Jul 18 '12 at 18:53

4 Answers4

2

Overloaded actions are not supported in MVC. The dispatcher can not tell the difference between the two Actions. You can get around this by giving one of your actions the [HttpGet] attribute and the other one the [HttpPost] attribute.

If that isn't an option (or if you have three or more overloads), you can always dispatch the Action yourself, by using an object parameter and using run time type identification to select the correct function to call. E.g.:

[HttpPost]
public ActionResult ItemUpdate(object arg)
{
    if (arg.GetType() == typeof(ORDER))
    {
        return ItemUpdateOrder((Order)arg);
    }
    else if (arg.GetType() == typeof(List<ORDER>))
    {
        return ItemUpdateList((List<Order>)arg);
    }
}

public ActionResult ItemUpdateOrder(ORDER ln)
{
    //...
}

public ActionResult ItemUpdateList(List<ORDER> lns)
{
    //...
}
Taedrin
  • 445
  • 1
  • 4
  • 16
1

You can't do that in a controller. You will have to change the second method name.

[HttpPost]
public ActionResult ItemUpdates(List<ORDER> lns)
{
  // Do Something
}
James
  • 2,823
  • 22
  • 17
0
public ActionResult ItemUpdates(object myInputValue)
{
    if (myInputValue.GetType() == typeof(string)
    // Do something
    else if (myInputValue.GetType() == typeof(List<ORDER>))
    // Do something else
}

You can then cast the object to your type of choice and manipulate normally.

Eric
  • 1,356
  • 2
  • 14
  • 24
0

In ASP.NET it's not possible to have overloaded methods without an ActionFilter attribute to distinguish these actions. The reason for this is that the ActionInvoker (a class used inside of the Controller base class to invoke actiosn) cannot determine which method to call since it would need to "ask" a ModelBinder (which are responsible to construct action argument objects) for every overload if the ModelBinder could construct that argument object from the HTTP request passed. For simple scenarios this would work but in more complex scenarios this would fail because the ModelBinder would succeed in binding arguments of multiple overloads. Not to allow overloads in ASP.NET MVC is quite clever design decision.

To solve your problems you can fix ItemUpdate HTTP GET action by just leaving the second action away and having just one action, because a ModelBinder does not mind if a value that is passed as URL parameter for example is a string or an int.

[HttpGet]
public ActionResult ItemUpdate(string name)
{
    return Content(name);
}

For the ItemUpdate HTTP POST version I'd recommend to rename one of these actions or to have only one action, the list version because updating a single ORDER is only a specific case of updating multiple ORDER objects.

saintedlama
  • 6,838
  • 1
  • 28
  • 46