을 지원하는 방법을 여러 형태 대상으로 하는 동 만들기 작업에서 동일한 페이지?
-
29-09-2020 - |
문제
내가 가지고 싶은 두 가지 별도의 양식에서 단 하나 만들기 페이지 하나의 작업에서 컨트롤러한 각각의 형태입니다.
에 view:
<% 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"
-오류 또는 잘못된 생성 작업이 호출됩니다.
솔루션?
- 을 결합한 그 두 게시 만들기 작업
중 하나로 공개
ActionResult Create(Product product, string genericInput);
- 이름 하나의 게시물을 만들기 작업과 다르게 추가하는 새로운 이름을 해당하는
Html.BeginForm()
내가 무슨 생각이 없는 사항에서 이러한.당신은 어떻게 이 문제를 해결?
해결책
실제로,나는 당신이 할 수 있다면 당신은 더욱 구체적으로 BeginForm()호출합니다.
Using(Html.BeginForm<ControllerName>(c => c.Create((Product)null)) { }
Using(Html.BeginForm<ControllerName>(c => c.Create((string)null)) { }
다른 팁
할 수 없습니다 두 개의 작업과 동일한 이름과 동사에만 다른 매개변수는 유형입니다.이럴의 이름을 지정하는 두 개의 행동을 다르게 될 것이 좋은 아이디어 가정하면 그들은 다른 작업을 수행하고 다른 입력이 있습니다.
제휴하지 않습니다 StackOverflow