Ambiguity elimination

I have a controller with 3 overloads for the create method:

public ActionResult Create() {} public ActionResult Create(string Skill, int ProductId) {} public ActionResult Create(Skill Skill, Component Comp) {} 

in one of my views I want to create this thing, so I call it this way:

 <div id="X"> @Html.Action("Create") </div> 

but I get the error:

{"The current Create action request for the XController controller type is ambiguous between the following methods: System.Web.Mvc.ActionResult Create () as the X.Web.Controllers.XController System.Web.Mvc.ActionResult Create (System.String , Int32) of type X.Web.Controllers.XController System.Web.Mvc.ActionResult Create (X.Web.Models.Skill, X.Web.Models.Component) by type X.Web.Controllers.XController "}

but since @html.Action() does not pass any parameters, the first overload should be used. This does not seem ambiguous to me (this means that I do not think it is a C # compiler).

can anyone point out the error of my ways?

+4
source share
2 answers

By default, overload methods are not supported in ASP.NET MVC. You must use delta actions or optional parameters. For instance:

 public ActionResult Create() {} public ActionResult Create(string Skill, int ProductId) {} public ActionResult Create(Skill Skill, Component Comp) {} 

will change to:

 // [HttpGet] by default public ActionResult Create() {} [HttpPost] public ActionResult Create(Skill skill, Component comp, string strSkill, int? productId) { if(skill == null && comp == null && !string.IsNullOrWhiteSpace(strSkill) && productId.HasValue) // do something... else if(skill != null && comp != null && string.IsNullOrWhiteSpace(strSkill) && !productId.HasValue) // do something else else // do the default action } 

OR

 // [HttpGet] by default public ActionResult Create() {} [HttpPost] public ActionResult Create(string Skill, int ProductId) {} [HttpPost] public ActionResult CreateAnother(Skill Skill, Component Comp) {} 

OR

 public ActionResult Create() {} [ActionName("CreateById")] public ActionResult Create(string Skill, int ProductId) {} [ActionName("CreateByObj")] public ActionResult Create(Skill Skill, Component Comp) {} 

See also this Q & A

+7
source

You can use the ActionName attribute to specify different action names for all three methods

+1
source

All Articles