简体   繁体   English

ASP.NET MVC多个注册表单提交问题

[英]ASP.NET MVC Multiple registration forms submitting issue

I have multiple button in one view for now. 我现在在一个view有多个button For example, Admin and Student . 例如, AdminStudent If I click the Admin button , the view will show a popup modal-dialog of the Admin registration form and the same goes to Student . 如果单击“ 管理员” button ,该视图将显示“ 管理员注册”表单的弹出modal-dialog ,并且该modal-dialog也会显示到“ 学生”

The following is the code that I written in cshtml : 以下是我用cshtml编写的代码:

    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary("", new { @class = "text-danger" })

    <div class="container">
    -- the button | admin --
    <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#AdminModal">Admin Modal</a>

    <div class="modal fade" id="AdminModal" style="width:auto">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <a href="#" class="close" data-dismiss="modal">&times;</a>
                    <h3 class="modal-title" >Admin Modal</h3>
                </div>
                <div class="modal-body">
                    <form id="AdminForm">

    -- with other textfields ---
    -- and DropDownList for selecting User role, eg. Admin/Student --

                    </form>
                </div>    
                <div class="modal-footer">
                    <a href="#" class="btn btn-default" data-dismiss="modal">Cancel</a>
                    <input type="reset" value="Submit" class="btn btn-success" id="btnSubmit" />
                </div>
            </div>
        </div>
    </div>
</div>



    <div class="container">
    -- the button | student --
    <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#StudentModal">Student Modal</a>

    <div class="modal fade" id="StudentModal" style="width:auto">
        <div class="modal-dialog">--student form content--<div>
    </div>
    </div>
    }

Provided with .Js : 提供.Js

$(document).ready(function () {
    $("#btnSubmit").click(function () {
        var AdminFormdata = $("#AdminForm").serialize();

        $.ajax({
            type: "POST",
            url: "/Account/Register",
            data: AdminFormdata,
            success: function (d) {
                console.log(d);
                $("#AdminModal").modal("hide");
            }
        })
    })
});

I tried with the Admin dialog , the above code does work to popup the particular dialog . 我尝试了“ Admin dialog ,以上代码确实可以弹出特定对话框 However when I click the Submit button, it does not get the appropriate data from the chosen registration form . 但是,当我单击“提交”按钮时,它没有从所选的注册form获取适当的数据。 What is the problem here? 这里有什么问题?

HTML does not allow to nest a <form> inside another <form> . HTML不允许将<form>嵌套在另一个<form> Move the <div class="modal "> outside of the @using(Html.BeginForm) block. <div class="modal "> @using(Html.BeginForm)块之外。

Something like this: 像这样:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
        @Html.AntiForgeryToken()
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#AdminModal">Admin Modal</a>
} @* close main form here! *@

<div class="modal fade" id="AdminModal" style="width:auto">
    @* ... *@
    <form id="AdminForm"> ... </form>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM