【发布时间】:2012-07-17 21:28:23
【问题描述】:
我需要 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());
}
当我调用 /Test/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'
}
【问题讨论】:
-
您遇到了什么错误?你想完成什么?
-
我认为您走在正确的道路上。使用不同的签名、不同的参数类型重载 C# 方法是可以接受的。你的编译器能识别 ORDER 对象吗?是否因为方法不返回任何内容而犹豫不决(您暂时可以返回 null)?
-
问题在于这是 MVC,请参阅:stackoverflow.com/questions/436866/…
标签: c# asp.net asp.net-mvc-3