开发者

Multiple form submits with RenderAction

开发者 https://www.devze.com 2023-02-28 06:05 出处:网络
I am using RenderAction to include the form below in my views: <%@ Control Language=\"C#\" Inherits=\"System.Web.Mvc.ViewUserControl<IEnumerable<SelectListItem>>\" %>

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开发者_高级运维?


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(){...});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号