同じページで同じ作成アクションをターゲットにする複数のフォームをサポートする方法は?
-
29-09-2020 - |
質問
単一の作成ページに2つの別々のフォームを持ち、各フォームのコントローラ内で1つのアクションを持ちたいです。
図中:
<% 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"
- エラーまたは間違った作成アクションが呼び出されます。
解?
- これら2つの投稿の作成アクションを組み合わせます
1つの公開
ActionResult Create(Product product, string genericInput);
に
- 名前の1つの名前の1つは異なる方法で、新しい名前を対応する
Html.BeginForm()
に追加します。
私はこれらの警告が何であるかわからない。あなたはこれをどのように解決しますか?
解決
実際には、BeginForm()呼び出しでより具体的な場合は、これを行うことができると思います。
Using(Html.BeginForm<ControllerName>(c => c.Create((Product)null)) { }
Using(Html.BeginForm<ControllerName>(c => c.Create((string)null)) { }
. 他のヒント
引数型だけが異なる、同じ名前と動詞を持つ2つのアクションを持つことはできません。あなたの2つの行動は異なるように命名され、彼らが異なるタスクを実行し、異なる入力を取ることを想定して良い考えでしょう。
所属していません StackOverflow