MVC客户端验证如何正常工作?

时间:2010-05-14 11:36:24

标签: asp.net asp.net-mvc validation client-side

我使用过MVC MVC 2.0客户端验证,但它没有按预期工作。现在我试图找出我做错了什么。 它是如何工作的? 我有这种渲染形式:

<form method="post" action="/Sprint/Edit/68d4886b-a86a-4f0b-b713-39219febddf3">

        <fieldset>

            <legend>Sprint</legend>

            <table>

<tbody><tr>
    <td><label for="sprint_Title">Title</label></td>
    <td><input type="text" value="Wichtige Private Erledigungen" name="sprint.Title" id="sprint_Title" class="input-box t-input"></td>
    <td><span class="error"><span id="sprint_Title_validationMessage" class="field-validation-valid"></span></span></td>
</tr>

<tr>
    <td><label for="sprint_Date">Date</label></td>
    <td>
        <div id="sprint_Date" class="date-picker t-input t-widget t-datepicker"><input value="12.05.2010" name="sprint.Date" id="sprint_Date-input" class="t-input" autocomplete="off"><a title="Open the calendar" tabindex="-1" href="#" class="t-link t-icon t-icon-calendar">select date</a></div>
    </td>
    <td><span class="error"><span id="sprint_Date_validationMessage" class="field-validation-valid"></span></span></td>
</tr>


<tr>
    <td><label for="sprint_Description">Description</label></td>
    <td><textarea rows="10" name="sprint.Description" id="sprint_Description" cols="80" class="t-input">dsfs1</textarea></td>
    <td><span class="error"><span id="sprint_Description_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
            </tbody></table>

        </fieldset>

    <input type="submit" value="Submit" name="Submit" id="Submit" class="t-button t-state-default">

</form>

这个脚本呈现:

<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"sprint.Title","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Title_validationMessage","ValidationRules":[{"ErrorMessage":"Title is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"sprint.Date","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Date_validationMessage","ValidationRules":[{"ErrorMessage":"Das Feld \"Date\" ist erforderlich.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"sprint.Description","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Description_validationMessage","ValidationRules":[]}],"FormId":null,"ReplaceValidationSummary":false});
//]]>
</script>

但令我惊讶的是,断点仅在页面加载时停止。我认为如果一个字段是标签的话也应该如此?至少,按下发布按钮后。

为什么客户端验证对我不起作用?

1 个答案:

答案 0 :(得分:0)

这只是设置处理程序,在提交表单时,实际上不会点击此行,也不会在字段之间切换。

在失去焦点和/或表单提交时,您将收到验证错误,但是如果页面上有其他javascript错误,则可能导致失败。

另外,请确保您在标题中引用了所需的javascript文件:

  • MicrosoftAjax.js
  • MicrosoftMvcValidation.js