Javascript 如果存在验证错误,防止引导模式关闭

Javascript 如果存在验证错误,防止引导模式关闭,javascript,jquery,validation,asp.net-mvc-5,bootstrap-modal,Javascript,Jquery,Validation,Asp.net Mvc 5,Bootstrap Modal,我在.NET MVC5应用程序中有一个引导模型。我的客户端验证正在工作(jquery在MVC中不引人注目),但问题是,即使有错误,我的模式也会一直关闭。如何防止模型关闭并仅显示错误?在我使用的javascript中,它阻止模式关闭,但不显示任何错误。我似乎只能让它在模态关闭时显示错误。我需要它保持开放,如果有验证错误 这是我的html: <div class="modal" id="createMessageModal" tabindex="

我在.NET MVC5应用程序中有一个引导模型。我的客户端验证正在工作(jquery在MVC中不引人注目),但问题是,即使有错误,我的模式也会一直关闭。如何防止模型关闭并仅显示错误?在我使用的javascript中,它阻止模式关闭,但不显示任何错误。我似乎只能让它在模态关闭时显示错误。我需要它保持开放,如果有验证错误

这是我的html:

<div class="modal" id="createMessageModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-xl" role="document">
        <div class="modal-content">
            @using (Html.BeginForm("Dashboard", "App", null, FormMethod.Post, new { @id = "frmSendMessage", @name = "frmSendMessage" }))
            {
                <div class="modal-header">
                    <h5 class="modal-title">Send Message</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                    <table>
                        <tbody>
                            <tr>
                                <th style="padding-bottom:15px">From:</th>
                                @Html.HiddenFor(model => model.messageSenderID)
                                <td style="padding-bottom:15px;">@ViewBag.Name</td>
                            </tr>
                            <tr>
                                <th style="padding-bottom:15px">To:</th>
                                <td style="width:100% !important; padding-bottom: 15px;">
                                    @Html.DropDownListFor(model => model.messageRecipientID, Model.messageRecipientsDropdown, "Select A Recipient", new { @id = "recipient", @name = "recipient", @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.messageRecipientID, "", new { @class = "text-danger" })
                                </td>
                            </tr>
                            <tr>
                                <th>Subject:</th>
                                <td style="width:100% !important">@Html.TextBoxFor(model => model.messageSubject, new { @class = "form-control", @name = "messageSubject", @id = "messageSubject" })</td>
                                <td>@Html.ValidationMessageFor(model => model.messageSubject, "", new { @class = "text-danger" })</td>
                            </tr>
                        </tbody>
                    </table>

                    <br />
                    @Html.TextAreaFor(model => model.messageBody, new { rows = "15", style = "resize:none; min-width:100%;", id = "messageBody", name = "messageBody" })
                    @Html.ValidationMessageFor(model => model.messageBody, "", new { @class = "text-danger" })
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Send Message</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>

                </div>
            }
        </div>
    </div>
</div>

在您的情况下,可能有其他代码干扰表单提交/模式,或者表单以某种方式提交并重新加载同一页面。这是我能想到的唯一两个原因

下面是使用代码作为基础从jquery执行验证的示例

这是一个简单的搜索查询提交给SO。您可以看到,如果未输入搜索文本,modal将保持打开状态

请注意
$(“#frmSendMessage”)内的注释。提交

$(文档).ready(函数(){
$(“#frmSendMessage”).submit(函数(){
var query=document.getElementById('myQuery');
如果(query.value==“”){
$('#vmsg').html(“请输入您的搜索查询”)
返回false;//表单将不提交,并且modal将保持打开状态
}
return true;//表单将被提交,并且由于页面被更改/重新加载,modal将关闭
})
});

表演形式
情态标题
&时代;
现在搜索
$(document).ready(function() {
  $("#frmSendMessage").submit(function() {
    Recipient = $("input[name='recipient']").val();
    MessageSubject = $("input[name='messageSubject']").val();
    MessageBody = $("input[name='messageBody']").val();


    return false;
  })
});