【问题标题】:Submitting form to two different actions in two different controllers asp.net mvc在两个不同的控制器asp.net mvc中将表单提交给两个不同的动作
【发布时间】:2016-12-11 11:01:52
【问题描述】:

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

 @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 是控制器。
在我不提交表单之前,最后的锚标记将不起作用

【问题讨论】:

  • 感谢编辑@Yogi
  • form 范围中移除 anchor tag。在表格右括号之后添加它..
  • 是的,在实际代码中它超出了表单范围。
  • 然后在这里发布实际代码..
  • 这是实际的代码先生。我无法理解 stackoverflow 如何发布代码。所以这是一个小错误。

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


【解决方案1】:

您可以使用 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>

【讨论】:

  • 我认为这不是一个好方法,提供 url 和字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 2016-09-26
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
相关资源
最近更新 更多