JQuery 模态弹出问题中的 ASP.net MVC 3 不引人注目的客户端验证

发布于 2024-12-07 11:15:11 字数 1523 浏览 1 评论 0原文

当我尝试在应用程序中实现 ASP.net MVC 3 Unobtrusive Client Validations 时,呈现的 html 没有生成由 JQuery 生成的 span 标签。

我只得到了以下渲染 html,并且我使用 JQuery 模式弹出窗口作为我的部分视图的容器。

<input data-val="true" data-val-required="The City field is required." id="City" name="City" type="text" value="Seattle" />

但是,当我在没有 JQuery Modal 弹出窗口的情况下使用不引人注目的客户端验证时,它在同一应用程序中可以正常工作,如下所示。

<div class="editor-field">
  <input data-val="true" data-val-required="The City field is required." id="City" name="City" type="text" value="Seattle" />
  <span class="field-validation-valid" data-valmsg-for="City" data-valmsg-replace="true"></span>
</div>

当我在 JQuery Modal Popup 中使用 Unobtrusive Client 验证时,有什么需要实现的吗?

JQuery 弹出代码

 $(document).ready(function () {
        $('#actionPanelDialogs div').dialog({
            autoOpen: false,
            modal: true,
            width: 700,
            appendToBody: true
        });


        $('#actions a').click(function (event) {
            event.stopPropagation();
            event.preventDefault();
            var link = $(this);
            var action = link.attr('href');
            var dialogDivId = link.attr('rel');

            var dialogDiv = $('#' + dialogDivId);

            $.get(action, null, function (data) {
                dialogDiv.html(data);

                dialogDiv.dialog('open');                

            });
            return false;
        });
 });

When I tried to implement ASP.net MVC 3 Unobtrusive Client Validations in the application, the rendered html didn't generate the span tags which are generated by JQuery.

I have only got following rendering html and I used JQuery modal popup as a container for my partial View.

<input data-val="true" data-val-required="The City field is required." id="City" name="City" type="text" value="Seattle" />

However, when I used Unobtrusive Client validation with out JQuery Modal pop up it works correctly as follows in same application.

<div class="editor-field">
  <input data-val="true" data-val-required="The City field is required." id="City" name="City" type="text" value="Seattle" />
  <span class="field-validation-valid" data-valmsg-for="City" data-valmsg-replace="true"></span>
</div>

Is there any thing that I need to implement when I use Unobtrusive Client validations in JQuery Modal Popup?

JQuery Popup Code

 $(document).ready(function () {
        $('#actionPanelDialogs div').dialog({
            autoOpen: false,
            modal: true,
            width: 700,
            appendToBody: true
        });


        $('#actions a').click(function (event) {
            event.stopPropagation();
            event.preventDefault();
            var link = $(this);
            var action = link.attr('href');
            var dialogDivId = link.attr('rel');

            var dialogDiv = $('#' + dialogDivId);

            $.get(action, null, function (data) {
                dialogDiv.html(data);

                dialogDiv.dialog('open');                

            });
            return false;
        });
 });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

依 靠 2024-12-14 11:15:11

您必须手动触发对添加到 DOM 的任何新元素的解析。

jQuery.validator.unobtrusive.parse("#modalPopup"); 

如果您使用的是 jQuery UI

$( ".selector" ).dialog({
   open: function(event, ui) {
      jQuery.validator.unobtrusive.parse(ui);
   }
});

You have to manually trigger a parse of any new elements added to the DOM.

jQuery.validator.unobtrusive.parse("#modalPopup"); 

If you are using jQuery UI

$( ".selector" ).dialog({
   open: function(event, ui) {
      jQuery.validator.unobtrusive.parse(ui);
   }
});
爱的那么颓废 2024-12-14 11:15:11

发现问题,

我使用表单 html 创建表单元素,如下所示。

<form  action="@Url.Action("Create", "Person")" enctype="multipart/form-data" method="post" id="contactForm">

当我使用 Html 助手时,它工作正常。

@{using (Html.BeginForm("Create", "Person", new { enctype = "multipart/form-data", id = "contactForm" }))

Found the problem,

I've used form html to create form element as follows.

<form  action="@Url.Action("Create", "Person")" enctype="multipart/form-data" method="post" id="contactForm">

When I used Html helper it worked correctly.

@{using (Html.BeginForm("Create", "Person", new { enctype = "multipart/form-data", id = "contactForm" }))
抱着落日 2024-12-14 11:15:11

对于任何使用引导模式的人,请使用以下代码:

$('#myModal').on('shown.bs.modal', function () {
    jQuery.validator.unobtrusive.parse($(this));
})

感谢 Josh 的勇气。

For anyone using bootstrap modals use the following code:

$('#myModal').on('shown.bs.modal', function () {
    jQuery.validator.unobtrusive.parse($(this));
})

Thanks to Josh for the guts of it.

屋檐 2024-12-14 11:15:11

尝试了所有方法,这对我有用:

$(".showModal").click(function () {
            var id = $(this).attr("data-id");
            $("#modal").load("/Controller/Action?id=" + id, function () {
                $("#modal").modal();
                $.validator.unobtrusive.parse($("#modal"));
            })
        });

Tried all approaches and this is what worked for me:

$(".showModal").click(function () {
            var id = $(this).attr("data-id");
            $("#modal").load("/Controller/Action?id=" + id, function () {
                $("#modal").modal();
                $.validator.unobtrusive.parse($("#modal"));
            })
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文