【问题标题】:Why is my form validation not working when I have my validation summary in place当我有我的验证摘要时,为什么我的表单验证不起作用
【发布时间】: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">&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; }
}

【问题讨论】:

  • 只是为了确认一下,您的应用程序中是否启用了客户端验证?
  • @Sam.C 像 javascript 中的验证?.. 不,我不
  • 要使 javascript 验证正常工作,您需要启用验证并将正确的 javascript 库添加到您的站点。 dotnet-tricks.com/Tutorial/mvc/… 如果已经设置好了请忽略。
  • 这仍然没有改变它没有在我的控制器中验证的事实

标签: asp.net-mvc forms validation


【解决方案1】:

在视图中,改变

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

【讨论】:

  • 你知道如何防止我的模态在出现错误时关闭,...此时验证显示但模态关闭
  • @ifelabolz closes 是什么意思?
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 2021-06-24
  • 1970-01-01
  • 2023-03-19
  • 2021-05-20
相关资源
最近更新 更多