使用 RenderAction 提交多个表单

发布于 2024-11-02 17:35:12 字数 938 浏览 4 评论 0原文

我正在使用 RenderAction 将下面的表单包含在我的视图中:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SelectListItem>>" %>
<% using (Html.BeginForm("form1", "UserControls", FormMethod.Post, new { @id = "form1" }))
{ %>
<div id="superDiv">Super User | Account:  <%= Html.DropDownList("dropdown", Model)%>
   <button type="submit" class="button">Button</button>
</div>
<% } %>

问题是,当我在视图中有表单时,例如:

<% using (Html.BeginForm("Reports", "Report", FormMethod.Post, new { @id = "reportForm", style= "display:none" }))
   { %>
   <%: Html.AntiForgeryToken()%>

  <%: Html.Hidden("reportType", null, new { @id = "reportType" }) %>
    <div class="middle"><button type="submit" class="button">Generate Report</button>
<% } %>

它将自动从 RenderAction 提交表单,如何防止这种情况发生?

I am using RenderAction to include the form below in my views:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SelectListItem>>" %>
<% using (Html.BeginForm("form1", "UserControls", FormMethod.Post, new { @id = "form1" }))
{ %>
<div id="superDiv">Super User | Account:  <%= Html.DropDownList("dropdown", Model)%>
   <button type="submit" class="button">Button</button>
</div>
<% } %>

The problem is that when I have a form in a view eg:

<% using (Html.BeginForm("Reports", "Report", FormMethod.Post, new { @id = "reportForm", style= "display:none" }))
   { %>
   <%: Html.AntiForgeryToken()%>

  <%: Html.Hidden("reportType", null, new { @id = "reportType" }) %>
    <div class="middle"><button type="submit" class="button">Generate Report</button>
<% } %>

It will auto submit the form from RenderAction, how can this be prevented?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

场罚期间 2024-11-09 17:35:12

也许你应该使用 Partial 而不是 RenderAction

我用它来处理 1 页上的两个表单(带有 2 个提交按钮):

第一个视图

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post)){...}

第二个视图

@using (Html.BeginForm("Register", "Account", FormMethod.Post)) {...}

第三个主视图:

<div style="...">
<div>
    @Html.Partial("Login")
</div>
<div>
    @Html.Partial("Register")
</div>

更新:

我需要将数据加载到用户控件中,并在多个视图/控件上使用它。它如何与部分一起工作?

您可以创建 Helper 来接受您的数据/参数并通过部分视图呈现,

public static MvcHtmlString MyRegisterUserControl(this HtmlHelper html, SomeClass data)
    {
        //maybe some calculations here
        return html.Partial("Register", data);
    }

然后您可以在视图中使用它(类似的东西):

@Html.MyRegisterUserControl(new SomeClass(){...});

Maybe you should use Partial instead of RenderAction

I use it to handle two forms (with 2 submit-buttons) on 1 page:

1st view

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post)){...}

2nd view

@using (Html.BeginForm("Register", "Account", FormMethod.Post)) {...}

3rd main view:

<div style="...">
<div>
    @Html.Partial("Login")
</div>
<div>
    @Html.Partial("Register")
</div>

upd:

I need to load data into the user control, and use it on multiple views/controls. How does that work with partial?

You can create Helper that will accepts your data/params and renders by partial views

public static MvcHtmlString MyRegisterUserControl(this HtmlHelper html, SomeClass data)
    {
        //maybe some calculations here
        return html.Partial("Register", data);
    }

then you can use it in your views (something like that):

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