【问题标题】:Multiple forms in one page MVC 5一页中的多个表单 MVC 5
【发布时间】:2014-11-03 09:07:24
【问题描述】:

我有一个 MVC 5 应用程序,在页脚中我有一个“订阅时事通讯”表单,问题是当我尝试添加另一个表单(注册用户,...)时,页脚中的表单也是已提交,请问如何解决问题,

<form id="newsletter-signup" action="@Url.Action("_SubscribeNewsletter", "Home")" method="post">

[HttpPost]
public ActionResult _SubscribeNewsletter(SubscribeNewsletterViewModel model)
{

@using (Html.BeginForm("Create", "Property", null, FormMethod.Post, new { @encType = "multipart/form-data", @id = "my-awesome-dropzone", @class = "add-estate", role = "form" }))
{

谢谢

【问题讨论】:

  • 您正在使用 javascript/jquery 提交表单????
  • 订阅时事通讯是的,但其他形式没有
  • 当您提交订阅时事通讯的形式时,请确保您在 jquery 中使用正确的选择器..就像这样..$("#newsletter-signup").submit() 而不会影响其他形式...

标签: razor asp.net-mvc-5


【解决方案1】:

您可以通过定义将哪个表单提交给哪个控制器操作方法来使用这种方法。

@using(Html.BeginForm("Login", "Controller", FormMethod.Post, new { id = "loginForm"}))
 {
       @Html.EditorFor(m => Model.Login)
 }

【讨论】:

    【解决方案2】:

    如果您不需要一次显示几个表单,您可以尝试隐藏包含表单的 div,这样它就好像它不存在并且您不应该遇到问题, 还可以尝试在表单中添加名称属性并检查提交按钮是否在它所属的表单内。

    这是我在一个 html 页面中制作的多个表单的一个小示例,我使用 javascript 来处理它并处理显示隐藏机制,但你应该能够只使用 css 来管理它

    <div class="ElementInformations" id="server">
                <h2>Server Informations</h2>
                <br />
                <form id="svform" action="">
                    <table>
                        ...
                    </table>
                    <br />
                    <input type="hidden" name="PrevSvid" id="PrevSvid" value="PrevSvid" />
                    <input class="content-button" type="button" onclick="putServerForm();" value="Save Server Changes" />
                    <br />
                    <br />
                    <input class="content-button" type="button" onclick="addDatabasePanel();" value="Add Database Panel" />
                    <br />
                    <br />
                    <input class="content-button" type="button" onclick="deleteServer();" value="Delete Server" />
                </form>
                <form id="newdatabaseform" action="">
                    <br />
                    <h2>Add Database</h2>
                    <br />
                    <table>
                        ...
                    </table>
                    <br />
                    <input type="hidden" id="NewServerId" name="ServerId" value="NewServerId" />
                    <input class="content-button" type="button" onclick="addDatabase();" value="Add Database" />
                </form>
            </div>
            <div class="ElementInformations" id="database">
                <h2>Database Informations</h2>
                <br />
                <form id="dbform" action="">
                    <table>
                        ...
                    </table>
                    <br />
                    <h2>Database Translation</h2>
                    <br />
                    <table>
                        ...
                    </table>
                    <br />
                    <input type="hidden" name="prevdbid" id="prevdbid" value="prevdbid" />
                    <input class="content-button" type="button" onclick="putDatabaseForm();" value="Save database modifications" />
                    <br />
                    <br />
                    <input class="content-button" type="button" onclick="deleteDatabase();" value="Delete Database" />
                </form>
            </div>
            <div id="NewServerPanel">
                <h2>Add Server</h2>
                <form id="newsvform" action="">
                    <table>
                      ...
                 </table>
                    <br />
                    <input class="content-button" type="button" onclick="addServer();" value="Add Server" />
                    <br />
                </form>
            </div>
    

    【讨论】:

    • @Html.TextBoxFor(model = > model.Email, new { @id = "mc4wp_email" })

    • MVC 的问题是你不能在一个视图中真正分离 2 个元素,因为模型是绑定的,即使你只需要一半,你也将始终拥有其中的所有内容,添加模块化考虑使用javascript 在服务器端发布模型之前对其进行格式化,如果您习惯于 javascript,请检查 jquery 或 angularjs
    • 如何获取单个表单的值,即仅获取 newdatabaseform 表单的值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多