2

하나의 작성 페이지에 두 개의 개별 양식과 각 양식의 제어기에있는 하나의 조치가 필요합니다. 뷰에서동일한 페이지에서 동일한 만들기 작업을 대상으로하는 여러 양식을 지원하는 방법은 무엇입니까?

:

<% using (Html.BeginForm()) { %> 
    // Contents of the first (EditorFor(Model.Product) form. 
    <input type="submit" /> 
<% } %> 
<% using (Html.BeginForm()) { %> 
    // Contents of the second (generic input) form. 
    <input type="submit" /> 
<% } %> 

컨트롤러에서 :

// Empty for GET request 
public ActionResult Create() { 
    return View(new ProductViewModel("", new Product())); 
} 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Create(Product product) { 

    return View(new ProductViewModel("", product)); 
} 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Create(string genericInput) { 
    if (/* problems with the generic input */) { 
     ModelState.AddModelError("genericInput", "you donkey"); 
    } 

    if (ModelState.IsValid) { 
     // Create a product from the generic input and add to database 
     return RedirectToAction("Details", "Products", new { id = product.ID }); 
    } 

    return View(new ProductViewModel(genericInput, new Product())); 
} 

"The current request for action 'MyMethod' on controller type 'MyController' is ambiguous between the following action methods"의 결과 - 오류 또는 잘못된 행동을 만들기가 호출됩니다.

솔루션?

  • 두 POST는 하나의 공용 ActionResult Create(Product product, string genericInput);
  • 이름을 다르게 작업을 만들기 POST 중 하나에 행동을 만들기 결합하고 나는 무엇 아무 생각이 해당 Html.BeginForm()

에 새 이름을 추가 이것들에주의하십시오. 어떻게 해결할 수 있니?

답변

1

사실, BeginForm() 호출에 더 구체적이라면이 작업을 수행 할 수 있다고 생각합니다.

Using(Html.BeginForm<ControllerName>(c => c.Create((Product)null)) { } 
Using(Html.BeginForm<ControllerName>(c => c.Create((string)null)) { } 
+0

안녕하세요! 정보 주셔서 감사합니다. 불행히도'Html.BeginForm' 확장 메소드의 제네릭 버전'Html.BeginForm '을 찾을 수 없었습니다. 이 확장 프로그램을 어디에서 찾을 수 있습니까? MVC 2와 3을 모두 시도했습니다. – randomguy

+0

MVC Futures. http://aspnet.codeplex.com/releases/view/41742에서 찾을 수 있습니다. – ARM

3

인수 유형과 만 다른 동일한 이름과 동사를 사용하여 두 가지 작업을 수행 할 수 없습니다. IMHO는 두 가지 행동을 다르게 명명하는 것은 그들이 다른 작업을 수행하고 다른 입력을한다고 가정 할 때 좋은 생각입니다.

관련 문제