c# - 在两个不同的 Controller asp.net mvc中将表单提交给两个不同的 Action

标签 c# asp.net asp.net-mvc forms

我在谷歌上搜索了很多关于这个问题的信息,但找不到关于我的场景的任何帮助。那么问题来了
我有一个包含单个下拉列表的表单,名为 Sections,用户可以选择一个Section,然后用户有两个选择。
1-通过转到文件上传表单将文件添加到此部分
2-通过添加部分表格添加更多小节
This is image of my form

enter image description here

 @using (Html.BeginForm("SectionForm","Sections"))
    {
    <div class="form-group">
        @Html.DisplayFor(s => s.SectionName)
        @Html.DropDownListFor(s => s.SelectedSection, new SelectList(Model.Sections,"SectionId","SectionName"),"Select a Section",new {@class = "form-control",autofocus="autofocus"} )
    </div>
    <p>
        If you don't see your desired section here, Click <strong>Add Section</strong>.
    </p>
    <button type="submit" class="btn btn-primary">Add Further Subsection</button>

      <br />
     <a href="@Url.Action("FileForm","LawFiles",new { sectionId =  Model.SelectedSection })" class="btn btn-primary">Add File</a>
}
<br>

SectionFormFileForm 是我的操作,SectionsLawFiles 是 Controller 。
在我不提交表单之前,末尾的 anchor 标记 将不起作用

最佳答案

您可以使用 javascript 修改表单的操作。您将在提交按钮上添加点击事件并覆盖默认提交操作。

 <html>
 <body>
    <form id="myform">
           <input type="submit" id="submit1"/>
           <input type="submit" id="submit2"/>
    </form>
 </body>
 <script>
    $("#submit1").click(function() {
    document.myform.action = “http://localhost/controller/action1”;
    });
    $("#submit2").click(function() {
      document.myform.action = “http://localhost/controller/action2”; 
    });
 </script>
</html>

关于c# - 在两个不同的 Controller asp.net mvc中将表单提交给两个不同的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782295/

相关文章:

c# - 使用对象列表填充 UserControl Gridview

c# - 将字典列表序列化为可接受的 DataTable Ajax 对象

c# - 如何在 LinkBut​​ton 单击事件上启动电子邮件客户端?

c# - 消息 : Invalid JSON primitive: ajax jquery method with Webmethod

c# - WCF web服务和getJSON为跨域调用返回空数据

c# - 实现一个可以异步调用web方法的Excel UDF

c# - 在 Windows 窗体中为文本框、复选框等任何控件加载类似事件?

javascript - 如何将 JWT 身份验证与 Web API 集成?

javascript - mvc $ ('form' ).submit 不在部分 View 上执行

asp.net-mvc - 引用来自不同项目的 Web api