如何支持同一页面上针对同一创建操作的多个表单?

发布于 2024-09-11 11:40:37 字数 1452 浏览 2 评论 0原文

我希望在一个创建页面上有两个单独的表单,并在控制器中为每个表单创建一个操作。

在视图中:

<% 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 public ActionResult
    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-09-18 11:40:37

不能有两个具有相同名称和动词且仅参数类型不同的操作。恕我直言,假设您的两个操作执行不同的任务并采用不同的输入,那么以不同的方式命名它们将是一个好主意。

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.

夏见 2024-09-18 11:40:37

实际上,我相信如果您对 BeginForm() 调用更加具体,您可以做到这一点。

Using(Html.BeginForm<ControllerName>(c => c.Create((Product)null)) { } 
Using(Html.BeginForm<ControllerName>(c => c.Create((string)null)) { }

Actually, I believe you can do this if you are more specific with your BeginForm() call.

Using(Html.BeginForm<ControllerName>(c => c.Create((Product)null)) { } 
Using(Html.BeginForm<ControllerName>(c => c.Create((string)null)) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文