简体   繁体   English

ASP.NET MVC验证不适用于bootstrap模式

[英]ASP.NET MVC validation not working on bootstrap modal

I can't get the bootstrap modal and asp.net mvc validation start working together. 我无法获得bootstrap模态和asp.net mvc验证开始协同工作。 I've got a complex form with some validation displayed in bootstrap modal. 我有一个复杂的表单,在bootstrap模式中显示了一些验证。 Unfortunetely when I hit the submit button the validation doesn't work at all. 不幸的是,当我点击提交按钮时,验证根本不起作用。

The form uses standard asp.net mvc validation. 该表单使用标准的asp.net mvc验证。 Below there is a part of it just to get the idea of how it is build: 下面是它的一部分,只是为了了解它是如何构建的:

@using (Html.BuildForm().AddClass("form-horizontal").Id("contact-add-popup").EncType(FormEncType.MultipartData).Begin()) {
@Html.AntiForgeryToken()
@Html.Partial("_Alerts")
<div class="control-group">

<div class="control-group company-field">
    @Html.BuildLabelFor(m => m.Name).AddClass("control-label")
    <div class="controls">
        @Html.BuildTextBoxFor(m => m.Name).AddClass("input-xxlarge")
        @Html.ValidationMessageFor(m => m.Name)
    </div>
</div>
(...)

Here is my modal: 这是我的模态:

<div id="createContactModal" class="modal hide fade modal-contact" tabindex="-1" role="dialog" aria-labelledby="createContactModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-header">
    <h4 class="modal-label" id="createContactModalLabel">Add contact</h4>
</div>
<div class="modal-body">
    @Html.Partial("_CreateContact", new ContactCreateModel())
</div>
<div class="modal-footer">
    <a href="javascript:$('#contact-add-popup').submit();" class="btn btn-primary">Zapisz</a>
    <button class="btn" data-dismiss="modal" aria-hidden="true">Zamknij</button>
</div>

And some javascript that I hope to get the validation working: 还有一些我希望验证工作的javascript:

        $('#createContactModal').on('shown', function () {
            $("#contact-add-popup").removeData("validator");
            $("#contact-add-popup").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse("#contact-add-popup");
        });

        $('#contact-add-popup').on('submit', function(e){
            e.preventDefault();

            $.validator.unobtrusive.parse($("#contact-add-popup"));

            if ($('#contact-add-popup').valid()){
                alert('AJAX');
            }
        }); 

The line if ($('#contact-add-popup').valid()) returns always true. 行if($('contact-add-popup')。valid())总是返回true。 How can I get the modal and validation to work? 如何使模态和验证工作?

You should try this way: 你应该这样试试:

var form = $("#contact-add-popup")
        .removeData("validator")
        .removeData("unobtrusiveValidation");

$.validator.unobtrusive.parse(form);

Stackoverflow: unobtrusive validation not working with dynamic content Stackoverflow:不引人注意的验证不使用动态内容

After some research I found that javascript validation script files were missing - so the client side validation was not working at all. 经过一些研究后,我发现javascript验证脚本文件丢失了 - 所以客户端验证根本不起作用。 After including these files everything works fine. 包含这些文件后一切正常。

Thanks for all answers. 谢谢你的所有答案。

Add this in the base view, load modal and enable client side validation. 在基本视图中添加它,加载模式并启用客户端验证。

@section scripts {

  @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }

  @* The normal bootstrap behavior is to only grab the content
     for the modal once, if you need to pull in different partial
     views then the data on the modal will have to be cleared. *@

  <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

  <script type="text/javascript">
    $(function () {
      $('#modal-container').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget); // Button that triggered the modal
        var url = button.attr("href");
        var modal = $(this);
        //enable client side validation after page is loaded
        modal.find('.modal-content').load(url, function () {
          $('#registration_form').removeData("validator");
          $('#registration_form').removeData("unobtrusiveValidation");
          $.validator.unobtrusive.parse('#registration_form');
        });
      });
    });
  </script>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM