2013-02-18 75 views
0

時,爲什麼會收到此錯誤錯誤顯示頁面-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 

控制器類:

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

     } 


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

      return View(); 
     } 


    } 
} 

回答

8

他們都是GET操作。如果你想重載需要添加屬性,改變你的方法名

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

    } 

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

     return View(); 
    } 


} 
1

[ActionName("OverloadedName")] 
你的第二個方法之前

補充一點:

[HttpPost] 

像這樣