使用ASP.Net Razor页面进行条件验证

时间:2019-01-29 18:44:13

标签: javascript c# asp.net .net-core bootstrap-4

我正在使用ASP.Net Core 2.2剃刀页面和Bootstrap 4,其中用户正在为每个家庭成员提供用户信息。使用相同的表单来添加成员和进行编辑,但是调用了不同的处理程序。如果他们不提供电子邮件,我想显示一个确认它的模式。使用我当前的代码,如果他们提供电子邮件,则可以正确提交。没有电子邮件时,模式显示,但按“是”则不执行任何操作,调试控制台中也不执行任何操作。

 function submitForm() {
        if ($("#email").val() === "") {
            $('#modalNoEmail').modal();
            return false;
        } else {
            document.getElementById("frm1").submit();
        }
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="col-md-8 order-md-1">    
      
        <form method="post" onsubmit="return submitForm()" id="frm1">

            <div class="modal" tabindex="-1" role="dialog" id="modalNoEmail">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title"> No Email Specified</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <p>
                                You have not entered an email address. Confirm proceeding without entering an email address?
                            </p>
                        </div>
                        <div class="modal-footer">
                            @if (Model.Subscriber != null && Model.CountOfSubscribers > 0)
                            {
                                <button class="btn btn-primary btn-secondary btn-block button" type="submit" asp-page-handler="Edit">Yes</button>
                            }
                            else
                            {
                                <button class="btn btn-primary btn-secondary btn-block button" type="submit" asp-page-handler="Add">Yes</button>
                            }
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                        </div>
                    </div>
                </div>
            </div>
            

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label>First name</label>
                    <input asp-for="Subscriber.FirstName" class="form-control">
                    <span asp-validation-for="Subscriber.FirstName" class="text-danger"></span>
                </div>
                <div class="col-md-6 mb-3">
                    <label>Last name</label>
                    <input asp-for="Subscriber.LastName" class="form-control">
                    <span asp-validation-for="Subscriber.LastName" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label>Email</label>
                    <input asp-for="Subscriber.Email" class="form-control" id="email">
                    <span asp-validation-for="Subscriber.Email" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    @if (Model.Subscriber != null && Model.CountOfSubscribers > 0)
                    {
                        <button class="btn btn-primary btn-lg btn-block button" type="submit" asp-page-handler="Edit">Save</button>

                    }
                    else
                    {
                        <button class="btn btn-primary btn-lg btn-block button" type="submit" asp-page-handler="Add">Add Member</button>
                    }
                </div>               
            </div>

        </form>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

也许我读错了,但看来您循环无穷。您正在提交回处理程序,该处理程序中的电子邮件仍然是一个空字符串,因此它将再次调用模式窗口。