-1

I have the following form which is a bootstrap form. when a validation error occurs, i get the validationsummary appearing which is good. but when i close the modal, I want to clear the summary. how can i do this ?.

@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { Id = "frmSendEmail", @class = "form-horizontal" }))
{
    <div class="modal fade" id="modalSendEmail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="modalLabel">Email</h4>
                </div>
                <div class="modal-body">
                    @Html.ValidationSummary(false, "Oops! Seems like we are missing the following details:", new { @class = "alert alert-danger" })
                    <div class="form-group">
                        <label for="txtName" class="col-sm-2 control-label">* Name:</label>
                        <div class="col-sm-10">
                            @Html.TextBoxFor(model => model.SenderName, null, new {id = "txtName", @class = "form-control", placeholder = "Name"})
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="txtEmail" class="col-sm-2 control-label">* Email:</label>
                        <div class="col-sm-10">
                            @Html.TextBoxFor(model => model.SenderEmail, null, new { id = "txtEmail", @class = "form-control", placeholder = "Email", type = "email" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="txtTelephone" class="col-sm-2 control-label">Telephone:</label>
                        <div class="col-sm-10">
                            @Html.TextBoxFor(model => model.SenderTelephone, null, new { id = "txtTelephone", @class = "form-control", placeholder = "Telephone" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="txtEnquiry" class="col-sm-2 control-label">* Enquiry:</label>
                        <div class="col-sm-10">
                            @Html.TextAreaFor(model => model.SenderEnquiry, new { id = "txtEnquiry", @class = "form-control", rows = "5" })
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="btnCloseSendEmail">Close</button>
                    <button type="submit" class="btn btn-warning" id="btnSendEmail">Send</button>
                </div>
            </div>
        </div>
    </div>
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
floormind
  • 1,868
  • 5
  • 31
  • 85

1 Answers1

1

Use hidden.bs.modal event to detect modal close. and this validation-summary-errors is the class that is added for validation errors div. so you can remove it, empty it or hide it as per your requirement.

$('#modalSendEmail').on('hidden.bs.modal', function (e) {      
    $('.validation-summary-errors').remove() 
    //or 
    //$('.validation-summary-errors').empty() 
    //or 
    //$('.validation-summary-errors').hide()
})
J Santosh
  • 3,808
  • 2
  • 22
  • 43
  • $('#modalSendEmail').on('hidden.bs.modal', function (e) { $('.field-validation-error').html(""); }) This has worked for me – hemanth gali Oct 08 '21 at 02:53