MVC 客户端验证导致错误

发布于 2024-09-12 09:48:15 字数 3770 浏览 6 评论 0原文

我希望有人遇到过类似的事情。 带有文本的文本输入触发客户端[必需]错误消息。

我在 master 中包含以下内容:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.js" type="text/javascript"></script>

以及

<% Html.EnableClientValidation(); %>

在 view 中包含以下内容。

几个可能的因素:

  • 表单处于部分视图中
  • 提交的文本来自 CKEditor
  • CKEditor 被隐藏 在页面加载时然后通过显示 jQuery

我只是不确定是什么原因造成的。

表单:

<form action="/Quote/Create/1" id="form0" method="post"><div class="validation-summary-valid" id="validationSummary"><ul><li style="display:none"></li>

</ul></div>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-field">
        <input id="QuoteID" name="QuoteID" type="hidden" value="0" />
    </div>
    <div class="editor-field">
        <input id="UserID" name="UserID" type="hidden" value="54cb0fde-649d-429c-8800-6bd9cc286d90" />
    </div>

        <div class="editor-label">
        <label for="PageNumber">PageNumber</label>
    </div>
    <div class="editor-field">
        <input id="PageNumber" name="PageNumber" type="text" value="" />
        <span class="field-validation-valid" id="PageNumber_validationMessage"></span>
    </div>
    <div class="editor-label">

        <label for="QuoteText">QuoteText</label>
    </div>
    <div class="editor-field">
        <textarea cols="20" id="QuoteText" name="QuoteText" rows="2">
</textarea>
        <span class="field-validation-valid" id="QuoteText_validationMessage"></span>
    </div>
    <div class="editor-field">
        <input id="BookID" name="BookID" type="hidden" value="1" />

    </div>
    <div class="editor-field">
        <input id="DateCreated" name="DateCreated" type="hidden" value="7/28/2010 9:21:36 PM" />
    </div>
    <p>
        <input id="quoteSubmit" type="submit" value="Create" />
    </p>
</fieldset>
</form><script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"PageNumber","ReplaceValidationMessageContents":true,"ValidationMessageId":"PageNumber_validationMessage","ValidationRules":[{"ErrorMessage":"The field PageNumber must be a number.","ValidationParameters":{},"ValidationType":"number"}]},{"FieldName":"QuoteText","ReplaceValidationMessageContents":true,"ValidationMessageId":"QuoteText_validationMessage","ValidationRules":[{"ErrorMessage":"That quote is too long.","ValidationParameters":{"minimumLength":0,"maximumLength":500},"ValidationType":"stringLength"},{"ErrorMessage":"You didn\u0027t even enter a quote...","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false,"ValidationSummaryId":"validationSummary"});
//]]>
</script>

当我取出

<% Html.EnableClientValidation(); %>

服务器端验证时,它工作正常并且不会产生错误。

I'm hoping someone has run into something similar. A text input with text is triggering the client-side [Required] error message.

I have the following included in the master:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.js" type="text/javascript"></script>

as well as having

<% Html.EnableClientValidation(); %>

in the view.

A couple possible factors:

  • The form is in a partial view
  • The submitted text is coming from CKEditor
  • The CKEditor is hidden
    on page load and then revealed via
    jQuery

I'm just not sure what could be causing this.

Form:

<form action="/Quote/Create/1" id="form0" method="post"><div class="validation-summary-valid" id="validationSummary"><ul><li style="display:none"></li>

</ul></div>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-field">
        <input id="QuoteID" name="QuoteID" type="hidden" value="0" />
    </div>
    <div class="editor-field">
        <input id="UserID" name="UserID" type="hidden" value="54cb0fde-649d-429c-8800-6bd9cc286d90" />
    </div>

        <div class="editor-label">
        <label for="PageNumber">PageNumber</label>
    </div>
    <div class="editor-field">
        <input id="PageNumber" name="PageNumber" type="text" value="" />
        <span class="field-validation-valid" id="PageNumber_validationMessage"></span>
    </div>
    <div class="editor-label">

        <label for="QuoteText">QuoteText</label>
    </div>
    <div class="editor-field">
        <textarea cols="20" id="QuoteText" name="QuoteText" rows="2">
</textarea>
        <span class="field-validation-valid" id="QuoteText_validationMessage"></span>
    </div>
    <div class="editor-field">
        <input id="BookID" name="BookID" type="hidden" value="1" />

    </div>
    <div class="editor-field">
        <input id="DateCreated" name="DateCreated" type="hidden" value="7/28/2010 9:21:36 PM" />
    </div>
    <p>
        <input id="quoteSubmit" type="submit" value="Create" />
    </p>
</fieldset>
</form><script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"PageNumber","ReplaceValidationMessageContents":true,"ValidationMessageId":"PageNumber_validationMessage","ValidationRules":[{"ErrorMessage":"The field PageNumber must be a number.","ValidationParameters":{},"ValidationType":"number"}]},{"FieldName":"QuoteText","ReplaceValidationMessageContents":true,"ValidationMessageId":"QuoteText_validationMessage","ValidationRules":[{"ErrorMessage":"That quote is too long.","ValidationParameters":{"minimumLength":0,"maximumLength":500},"ValidationType":"stringLength"},{"ErrorMessage":"You didn\u0027t even enter a quote...","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false,"ValidationSummaryId":"validationSummary"});
//]]>
</script>

When I take out the

<% Html.EnableClientValidation(); %>

server-side validation works fine and generates no error.

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

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

发布评论

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

评论(1

岁月如刀 2024-09-19 09:48:15

我想通了!

$('#quoteSubmit').click(function () {
CKEDITOR.instances['QuoteText'].updateElement();
});

哎哟。

I figured it out!

$('#quoteSubmit').click(function () {
CKEDITOR.instances['QuoteText'].updateElement();
});

Yeehaw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文