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

问题描述 投票:0回答:2

我在 .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:

$(document).ready(function() {
  $("#frmSendMessage").submit(function() {
    Recipient = $("input[name='recipient']").val();
    MessageSubject = $("input[name='messageSubject']").val();
    MessageBody = $("input[name='messageBody']").val();


    return false;
  })
});
javascript jquery validation asp.net-mvc-5 bootstrap-modal
2个回答
0
投票

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

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

这是向 SO 提交的简单搜索查询。您可以看到,如果未输入搜索文本,模式仍保持打开状态。

请注意里面的评论

$("#frmSendMessage").submit

$(document).ready(function() {
  $("#frmSendMessage").submit(function() {
    var query = document.getElementById('myQuery');
    if (query.value == "") {
      $('#vmsg').html("Please enter your search query")
      return false; //form will not submit and modal will remain open
    }
    return true; //form will get submitted and modal will close due to page being changed/reloaded
  })
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#formModal">
  Show Form
</button>

<div id="formModal" class="modal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form id="frmSendMessage" method="get" action="https://stackoverflow.com/search">
          <input type="text" id="myQuery" name="q" placeholder="your search query here">
          <div id="vmsg"></div>
          <button type="submit" name="button">Search Now</button>

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


0
投票

在打开模式之前运行以下命令

$(".validation-summary-errors").empty();  
$(".field-validation-error").empty();
© www.soinside.com 2019 - 2024. All rights reserved.