4

create メソッドの 3 つのオーバーロードを持つコントローラーがあります。

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

私の見解の 1 つで、私はこれを作成したいので、次のように呼び出します。

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

しかし、私はエラーが発生します:

{"コントローラ タイプ 'XController' でのアクション 'Create' に対する現在のリクエストは、次のアクション メソッド間であいまいです: タイプ X.Web.Controllers.XController での System.Web.Mvc.ActionResult Create() System.Web.Mvc.ActionResultタイプ X.Web.Controllers.XController での Create(System.String, Int32) System.Web.Mvc.ActionResult タイプ X.Web.Controllers での Create(X.Web.Models.Skill, X.Web.Models.Component)。 XController"}

しかし、@html.Action()はパラメーターを渡さないため、最初のオーバーロードを使用する必要があります。私にはあいまいではないようです (これは、私が ac# コンパイラのように考えていないことを意味するだけです)。

誰かが私のやり方の誤りを指摘できますか?

4

2 に答える 2

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
}

また:

// [HttpGet] by default
public ActionResult Create() {}

[HttpPost]
public ActionResult Create(string Skill, int ProductId) {}

[HttpPost]
public ActionResult CreateAnother(Skill Skill, Component Comp) {}

また:

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

こちらのQ&Aもご覧ください

于 2011-10-20T06:50:03.843 に答える
1

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

于 2011-10-20T06:50:21.053 に答える