0

このエラーが発生する理由

The current request for action 'Index' on controller type 'MyController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index() on type MyProj.Controllers.MyController
System.Web.Mvc.ActionResult Index(MyProj.Models.MyModel) on type MyProj.Controllers.MyController

コントローラ クラス:

public class MyController : Controller
    {
        //
        // GET: //
        public ActionResult Index()
        {
            return View();

        }


        public ActionResult Index(MyModel model)
        {
            string x = "Hello "+ model.name;

            return View();
        }


    }
}
4

2 に答える 2

8

どちらも GET アクションです。

これを 2 番目の方法の前に追加します。

[HttpPost]

そのようです:

public class MyController : Controller
{
    //
    // GET: //
    public ActionResult Index()
    {
        return View();

    }

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        string x = "Hello "+ model.name;

        return View();
    }


}
于 2013-02-18T15:04:00.827 に答える
1

オーバーロードしたい場合は、属性を追加してメソッド名を変更する必要があります。

[ActionName("OverloadedName")]
于 2013-02-18T15:05:04.100 に答える