显示页面-MVC初学者时出错

时间:2013-02-18 15:02:50

标签: c# asp.net asp.net-mvc

为什么我会收到此错误

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

COntroller类:

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

        }


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

            return View();
        }


    }
}

2 个答案:

答案 0 :(得分: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();
    }


}

答案 1 :(得分:1)

如果要重载,则需要添加属性以更改方法名称:

[ActionName("OverloadedName")]