如何支持同一页面上针对同一创建操作的多个表单?
我希望在一个创建页面上有两个单独的表单,并在控制器中为每个表单创建一个操作。
在视图中:
<% 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()));
}
导致 “控制器类型 'MyController' 上的操作 'MyMethod' 的当前请求在以下操作方法之间不明确”
- 错误或调用错误的 Create 操作。
解决方案?
- 结合这两个 POST Create 操作 到一个公共
ActionResult 创建(产品产品,字符串 genericInput);
- 以不同的方式命名其中一个 POST Create 操作,并将新名称添加到相应的
Html.BeginForm()
我不知道其中有什么注意事项。你会如何解决这个问题?
I want to have two separate forms on a single create page and one action in the controller for each form.
In the 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" />
<% } %>
In the controller:
// 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()));
}
Results in "The current request for action 'MyMethod' on controller type 'MyController' is ambiguous between the following action methods"
- error or the wrong Create action is called.
Solutions?
- Combine those two POST Create actions
into one publicActionResult
Create(Product product, string
genericInput); - Name one of the POST Create actions differently and add the new name to the corresponding
Html.BeginForm()
I have no idea what are the caveats in these. How would you solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能有两个具有相同名称和动词且仅参数类型不同的操作。恕我直言,假设您的两个操作执行不同的任务并采用不同的输入,那么以不同的方式命名它们将是一个好主意。
You cannot have two actions with the same name and verb that differ only with argument types. IMHO naming your two actions differently would be a good idea assuming that they perform different tasks and take different inputs.
实际上,我相信如果您对 BeginForm() 调用更加具体,您可以做到这一点。
Actually, I believe you can do this if you are more specific with your BeginForm() call.