我想在一个创建页面上有两个单独的表单,并且每个表单在控制器中都有一个操作。
在视图中:
<% 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 Create 操作合并为一个公共
ActionResult Create(Product product, string genericInput);
- 以不同的方式命名 POST Create 操作之一并将新名称添加到相应的
Html.BeginForm()
我不知道这些有什么警告。你会如何解决这个问题?