MVC:打开和关闭一次后,客户端验证在 SimpleModal AJAX 弹出窗口中停止工作

发布于 2024-09-15 00:12:05 字数 3495 浏览 7 评论 0原文

我有一个带有简单模式弹出窗口的视图。您单击超链接,它会弹出窗口,如果您尝试提交弹出表单而不填写任何信息,它将在文本框旁边显示相应的验证错误消息。如果您关闭模式弹出窗口并再次单击超链接,则当您单击提交时似乎不会进行客户端验证。它只是让它被提交,然后它会在服务器端检查时捕获它。为什么打开模式弹出窗口然后关闭它,然后再次重新打开会使验证停止工作?我正在使用在 SimpleModal 演示 中演示的基本 osx 模式。任何建议将不胜感激。我已经检查过该网站,但如果有人以前发表过可能有帮助的帖子,我也会感激不已。

这是我的部分视图文件:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NSS.Models.Company>" %>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<fieldset>
    <legend>New Company</legend>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Name)%>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Name)%>
        <%: Html.ValidationMessageFor(model => model.Company_Name)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Phone)%>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Phone)%>
        <%: Html.ValidationMessageFor(model => model.Company_Phone)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Fax)%>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Fax)%>
        <%: Html.ValidationMessageFor(model => model.Company_Fax)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Website)%>
    </div>

    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Website)%>
        <%: Html.ValidationMessageFor(model => model.Company_Website)%>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

该部分在 .aspx 块中呈现:

<div id='container'>
    <div id='content'>
        <div id='osx-modal'>
            <input type='button' name='osx' value='Demo' class='osx demo'/>
        </div>

        <!-- modal content -->
        <div id="osx-modal-content">
            <div id="osx-modal-title">Create a new Company</div>
            <div class="close"><a href="#" class="simplemodal-close">x</a></div>
            <div id="osx-modal-data">
                <h2>Create a new Company</h2>
                <% Html.RenderPartial("CreateForm", new NSS.Models.Company()); %>
                <p><button class="simplemodal-close">Close</button> <span>(or press ESC or click the overlay)</span></p>
            </div>
        </div>
    </div>
</div>

这是我的验证代码:

public class Company_Validation
{
    [Required(AllowEmptyStrings=false, ErrorMessage = "Company Name is required")]
    [StringLength(50, ErrorMessage = "Company Name may not be longer then 50 characters")]
    public string Company_Name { get; set; }
}

I have a view with a simplemodal popup window. You click on a hyperlink, it pops up the window, and if you try to submit the popup'd form without filling in any information it will display the appropriate validation error message next to the textbox. If you close the modal popup and click the hyperlink again, it seems to not do the client side validation when you click the submit. It just lets it be submitted, and then it'll catch it on the server side checking. Why would opening a modal popup and then closing it, and then reopening a second time make validation stop working? I'm using the basic osx modal that is demo'd at SimpleModal Demos. Any suggestions would be greatly appreciated. I've checked around the site but if someone has a previous post that could be helpful that would be appreciated too.

Here is my partial view file:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NSS.Models.Company>" %>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<fieldset>
    <legend>New Company</legend>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Name)%>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Name)%>
        <%: Html.ValidationMessageFor(model => model.Company_Name)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Phone)%>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Phone)%>
        <%: Html.ValidationMessageFor(model => model.Company_Phone)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Fax)%>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Fax)%>
        <%: Html.ValidationMessageFor(model => model.Company_Fax)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Company_Website)%>
    </div>

    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Company_Website)%>
        <%: Html.ValidationMessageFor(model => model.Company_Website)%>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

That partial is rendered in this block of .aspx:

<div id='container'>
    <div id='content'>
        <div id='osx-modal'>
            <input type='button' name='osx' value='Demo' class='osx demo'/>
        </div>

        <!-- modal content -->
        <div id="osx-modal-content">
            <div id="osx-modal-title">Create a new Company</div>
            <div class="close"><a href="#" class="simplemodal-close">x</a></div>
            <div id="osx-modal-data">
                <h2>Create a new Company</h2>
                <% Html.RenderPartial("CreateForm", new NSS.Models.Company()); %>
                <p><button class="simplemodal-close">Close</button> <span>(or press ESC or click the overlay)</span></p>
            </div>
        </div>
    </div>
</div>

Here is my Validation code:

public class Company_Validation
{
    [Required(AllowEmptyStrings=false, ErrorMessage = "Company Name is required")]
    [StringLength(50, ErrorMessage = "Company Name may not be longer then 50 characters")]
    public string Company_Name { get; set; }
}

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

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

发布评论

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

评论(1

野侃 2024-09-22 00:12:05

由于 SimpleModal 处理模式内容的方式,听起来它正在失去验证绑定。

一种选择是使用 persist: true 选项,但您可能希望确保清除所有填充的表单值。

例如:

$(element).modal({
    persist: true,
    onClose: function (dialog) {
        var modal = this;
        $('input, textarea', dialog.data[0]).val('');
        modal.close();
    }
});

Because of the way SimpleModal handles the contents of the modal, it sounds like it is losing the validation binding.

One option would be to use the persist: true option, but you'd probably want to make sure to clear out any populated form values.

For example:

$(element).modal({
    persist: true,
    onClose: function (dialog) {
        var modal = this;
        $('input, textarea', dialog.data[0]).val('');
        modal.close();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文