Asp.net mvc 2 如何支持在同一页面上针对同一创建操作的多个表单?
我希望在一个创建页面上有两个单独的表单,在控制器中为每个表单执行一个操作 他认为:Asp.net mvc 2 如何支持在同一页面上针对同一创建操作的多个表单?,asp.net-mvc-2,model-binding,controller-action,html-post,Asp.net Mvc 2,Model Binding,Controller Action,Html Post,我希望在一个创建页面上有两个单独的表单,在控制器中为每个表单执行一个操作 他认为: <% using (Html.BeginForm()) { %> // Contents of the first (EditorFor(Model.Product) form. <input type="submit" /> <% } %> <% using (Html.BeginForm()) { %> // Contents of th
<% 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" />
<% } %>
导致“控制器类型'MyController'上的当前操作请求'MyMethod'在以下操作方法之间不明确”
-错误或调用错误的创建操作
解决方案
- 合并这两个创建后操作
进入一个公共
ActionResult 创建(产品、字符串) 一般输入)代码>
- 以不同的方式命名一个创建后操作,并将新名称添加到相应的
Html.BeginForm()
我不知道这里面有什么警告。您将如何解决这个问题?您不能有两个名称和动词相同但参数类型不同的动作。我认为,如果两个操作执行不同的任务并接受不同的输入,那么对它们进行不同的命名将是一个好主意。事实上,我相信如果您对BeginForm()调用更具体一些,您可以这样做
使用(Html.BeginForm(c=>c.Create((Product)null)){
使用(Html.BeginForm(c=>c.Create((string)null)){}
嘿!谢谢你提供的信息。不幸的是,我找不到Html.BeginForm
扩展方法的通用版本Html.BeginForm
。你能告诉我在哪里可以找到这个扩展吗?我两个都试过了,MVC 2和MVC 3。
// 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()));
}
Using(Html.BeginForm<ControllerName>(c => c.Create((Product)null)) { }
Using(Html.BeginForm<ControllerName>(c => c.Create((string)null)) { }