首页 文章

一页MVC 5中的多个表单

提问于
浏览
1

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

表格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“})){

谢谢

2 回答

  • 1

    如果您不需要一次显示几个表单,您可以尝试隐藏包含表单的div,这样就会不会存在并且您不应该遇到问题,同时尝试添加name属性您的表单并检查提交按钮是否在其所属的表单内 .

    这里是我制作的一个html页面中的多个表单的一个小样本,我使用javascript来处理它并处理show-hide机制但是你应该只能用css来管理它

    <div class="ElementInformations" id="server">
                <h2>Server Informations</h2>
                
    <form id="svform" action=""> <table> ... </table>
    <input type="hidden" name="PrevSvid" id="PrevSvid" value="PrevSvid" /> <input class="content-button" type="button" onclick="putServerForm();" value="Save Server Changes" />

    <input class="content-button" type="button" onclick="addDatabasePanel();" value="Add Database Panel" />

    <input class="content-button" type="button" onclick="deleteServer();" value="Delete Server" /> </form> <form id="newdatabaseform" action="">
    <h2>Add Database</h2>
    <table> ... </table>
    <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>
    <form id="dbform" action=""> <table> ... </table>
    <h2>Database Translation</h2>
    <table> ... </table>
    <input type="hidden" name="prevdbid" id="prevdbid" value="prevdbid" /> <input class="content-button" type="button" onclick="putDatabaseForm();" value="Save database modifications" />

    <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>
    <input class="content-button" type="button" onclick="addServer();" value="Add Server" />
    </form> </div>
  • 1

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

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

相关问题