简体   繁体   English

这是路由问题吗?

[英]Is this a routing issue?

I have developed an HTML form wizard that helps creating an object compose the data to send to the server. 我已经开发了一个HTML表单向导,该向导可以帮助创建一个对象,该对象将数据发送到服务器。

I have a controller with two action methods as follow: 我有一个具有以下两种操作方法的控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveItem( TypeOneItem model, int customerID ) {

and

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveItem( TypeTwoItem model, int customerID ) {

TypeOneItem and TypeTwoItem are both subclass of BaseItem. TypeOneItem和TypeTwoItem都是BaseItem的子类。 The class are defined this way 用这种方式定义类

public class BaseItem 
{
    public int ItemID { get; set; }
    public string Name { get; set; }
}

public class TypeOneItem : BaseItem
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

public class TypeTwoItem : BaseItem
{
    public string Property3 { get; set; }
    public int Property4 { get; set; }
    public double Property5 { get; set; }
}

However, even if I am posting the right data to be binded to just one of the two concrete classes I get the error " The current request for action 'SaveItem' on controller type 'ManageController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult SaveItem(TypeOneItem, Int32) System.Web.Mvc.ActionResult SaveItem(TypeTwoItem, Int32) " 但是,即使我发布正确的数据以将其仅绑定到两个具体类之一,我也会收到错误消息“ The current request for action 'SaveItem' on controller type 'ManageController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult SaveItem(TypeOneItem, Int32) System.Web.Mvc.ActionResult SaveItem(TypeTwoItem, Int32)

Why does this happens? 为什么会这样? I do not want to change the server action name and hard code the difference on the UI wizard. 我不想在UI向导上更改服务器操作名称和硬编码的区别。 Is there another way to solve this problem? 有没有其他方法可以解决此问题?

MVC supports method overloading based on attributes but it wont do it solely based on different method signatures. MVC支持基于属性的方法重载,但它不会仅基于不同的方法签名来做到这一点。 You can look at for reference ASP.NET MVC ambiguous action methods 您可以查看ASP.NET MVC歧义操作方法以供参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM