使用RenderAction提交多个表单

时间:2011-04-22 11:05:01

标签: asp.net-mvc renderaction

我正在使用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提交表单,如何防止这种情况?

1 个答案:

答案 0 :(得分:1)

也许您应该使用Partial代替RenderAction

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

第一个视图

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

第二视图

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

第3个主要观点:

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

<强> UPD:

我需要将数据加载到用户控件中,并在多个视图/控件上使用它。如何使用partial?

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

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

然后你可以在你的观点中使用它(类似的东西):

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