【发布时间】:2021-01-26 08:24:22
【问题描述】:
我在 .NET MVC5 应用程序中有一个引导模型。我的客户端验证正在工作(在 MVC 中 jquery 不显眼),但问题是即使出现错误,我的模式也会继续关闭。如何防止模型关闭并仅显示错误?在我拥有的 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">×</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