可能重复:
Can you overload controller methods in ASP.Net MVC?ASP.net C#,同名方法,它有不同的参数类型
我需要2种方法是采用不同类型的参数。所以我试过这个,
[HttpPost]
public ActionResult ItemUpdate(ORDER ln)
{
// do something
}
[HttpPost]
public ActionResult ItemUpdate(List<ORDER> lns)
{
// Do Something
}
但它不起作用。
编译时没有错误,但运行时会产生错误。
我如何编写代码以使其工作?
谢谢!
[编辑]
[HttpGet]
public ActionResult ItemUpdate(string name)
{
return Content(name);
}
[HttpGet]
public ActionResult ItemUpdate(int num)
{
return Content(num.ToString());
}
,当我打电话/测试/ ItemUpdate/
它使一个错误,
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
[编辑]
不匹配与ORDER甚至单个参数。
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'
}
你得到了什么错误?你想达到什么目的? – 2012-07-18 18:49:22
我认为你走在正确的道路上。可以使用不同的签名来重载C#方法,比如不同的参数类型。你的编译器是否能识别ORDER对象?它是否因为方法不返回任何内容而出现问题(您可以暂时返回null)? – DOK 2012-07-18 18:52:33
问题是,这是MVC,请参阅:http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – CaffGeek 2012-07-18 18:53:51