2011-10-20 14 views
4

解決曖昧

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

が、私はエラーを取得する:

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

しかし@html.Action()がパラメータを渡していないので、最初のオーバーロードを使用する必要があります。私にはあいまいではありません(私はC#コンパイラのようには思えません)。

誰でも私の方法のエラーを指摘できますか?

答えて

7

デフォルトでは、オーバーロードメソッドはASP.NET MVCではサポートされていません。差分アクションまたはオプションのパラメータを使用する必要があります。たとえば:

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

意志は変わりへ:

// [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

+0

はい、私は慎重な返信をいただきありがとうございます。あなたはポイントを得る:) – ekkis

+0

あなたは歓迎です:答えを受け入れるためにD ansありがとう –

+0

リンクされた質問が残念です。 7536119/mvc3-routing-with-overloaded-query-parameters – Maggie

1

ActionName属性を使用して、3つのすべてのメソッドに異なるアクション名を指定することができます。

+0

ありがとうございますAnkur、あなたはポイントを得る:) – ekkis