2012-10-18 2 views
2

MVC4를 사용하여 고객 객체를 나열, 생성, 편집 및 삭제할 수있는 간단한 테스트 웹 사이트를 만들려고합니다.MVC4 [HttpGet] 및 [HttpPost] 속성을 무시함

내 컨트롤러 안에는 컨트롤을로드 할 때 가져 오기 및 실제로 데이터를 저장하는 포스트가 있습니다.

The current request for action 'Create' on controller type 'CustomerController' is ambiguous between the following action methods: 
System.Web.Mvc.ActionResult Create() on type [Project].Controllers.CustomerController 
System.Web.Mvc.ActionResult Create([Project].Models.Customer) on type [Project].Controllers.CustomerController 

내가 내 가져 오기 및 게시 방법의 차이를 볼 수없는 것을 이해 내 : 내가 프로젝트를 실행하고 만드는 작업을 액세스 할 때

// 
    // GET: /Customer/Create 

    [HttpGet] 
    public ActionResult Create() 
    { 
     return View(); 
    } 

    // 
    // POST: /Customer/Create 

    [HttpPost] 
    public ActionResult Create(Customer cust) 
    { 
     if (ModelState.IsValid) 
     { 
      _repository.Add(cust); 
      return RedirectToAction("GetAllCustomers"); 
     } 

     return View(cust); 
    } 

그러나 나는 그 오류가 ,하지만 그 속성을 추가했습니다. 이 문제의 원인이 무엇이고 어떻게 다시 작동시킬 수 있습니까?

+0

프로젝트를 지우고 다시 작성하십시오. –

+0

과부하 또는 속성에 대한 속성을 acceptverbs에 추가해야합니다. http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Zaki

+0

이 코드는 괜찮아 보입니다. http 속성을 추가 한 후에 오류가 발생합니까? –

답변

2

MVC는 동일한 이름의 2 가지 작업 메서드를 가질 수있는 권한을 부여하지 않습니다.

하지만 http 동사가 다른 경우 (GET, POST) 동일한 URI를 사용하여 두 가지 작업 방법을 가질 수 있습니다. ActionName 속성을 사용하여 작업 이름을 설정하십시오. 동일한 메소드 이름을 사용하지 마십시오. 모든 이름을 사용할 수 있습니다. 규약은 HTTP 동사를 메서드 접미사로 추가하는 것입니다.

[HttpPost] 
[ActionName("Create")] 
public ActionResult CreatePost(Customer cust) 
{ 
    if (ModelState.IsValid) 
    { 
     _repository.Add(cust); 
     return RedirectToAction("GetAllCustomers"); 
    } 

    return View(cust); 
}