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 动作。

在第二种方法之前添加:

[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 回答