【发布时间】:2015-10-15 16:10:35
【问题描述】:
我的表单中有一个@HTML.ValidationSummary,它位于模态(对话框)中,但是,在模态关闭之前,它似乎没有检查表单输入是否具有有效值。我希望显示验证摘要,如下所示,我的视图模型的属性上有验证属性。
@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">×</span></button>
<h4 class="modal-title" id="modalLabel">Email</h4>
</div>
<div class="modal-body">
@Html.ValidationSummary("Oops! Seems like we are missing some information, please provide details for the required field marked with a *", 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"})
@Html.ValidationMessageFor(model => model.SenderName)
</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" })
@Html.ValidationMessageFor(model => model.SenderEmail)
</div>
</div>
<div class="form-group">
<label for="txtTelephone" class="col-sm-2 control-label">Telephone:</label>
<div class="col-sm-10">
@Html.TextBox("telephone", 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" })
@Html.ValidationMessageFor(model => model.SenderEnquiry)
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-warning" id="btnSendEmail">Send</button>
</div>
</div>
</div>
</div>
}
**Model**
public class Enquiry
{
[Required]
public string SenderName { get; set; }
[Required]
public string SenderEmail { get; set; }
public string SenderTelephone { get; set; }
[Required]
public string SenderEnquiry { get; set; }
}
【问题讨论】:
-
只是为了确认一下,您的应用程序中是否启用了客户端验证?
-
@Sam.C 像 javascript 中的验证?.. 不,我不
-
要使 javascript 验证正常工作,您需要启用验证并将正确的 javascript 库添加到您的站点。 dotnet-tricks.com/Tutorial/mvc/… 如果已经设置好了请忽略。
-
这仍然没有改变它没有在我的控制器中验证的事实
标签: asp.net-mvc forms validation