当我的验证摘要到位时,为什么我的表单验证不起作用

时间:2015-10-15 16:10:36

标签: asp.net-mvc forms validation

我的表单中有@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">&times;</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; }
}

1 个答案:

答案 0 :(得分:0)

在视图中,更改

@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" })

@Html.ValidationSummary(true, "", new { @class = "alert alert-danger" })

在控制器中:

public ActionResult Index(Enquiry emailEnquiry)
{
   if (ModelState.IsValid)
   {
      // all done
      return ...
   }   
   ModelState.AddModelError("", "Something is invalid.");
   return View(emailEnquiry); // don't forget returning with model
}