ASP.NET MVC3客户端验证帮助

时间:2011-07-06 21:36:54

标签: asp.net-mvc-3 jquery-validate client-side-validation unobtrusive-validation

我正在为我当前的应用程序使用不显眼的客户端验证,但问题是它没有验证所有字段,只验证其中一些字段。首先我认为DropDownLists是验证器省略的,但在更改了简单的TextBox之后,我意识到它也不起作用。我真的不知道它是什么。所以,我希望你能帮我一把:

我已经在我的网站上了.Config:

<appSettings>
  <add key="ClientValidationEnabled" value="true"/>
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

我有一个无法正常工作的类的元数据:

[MetadataType(typeof(QuestionMetadata))]
public partial class Question
{
    [Bind(Exclude = "Id")]
    public class QuestionMetadata
    {
        [Required]
        public string Text { get; set; }

        [Required]
        [DisplayName("Question Type")]
        public int QuestionType_Id { get; set; }

        [Required]
        [DisplayName("Category")]
        public int Category_Id { get; set; }

        [Required]
        [Range(1,Int32.MaxValue)]
        public int SortOrder { get; set; }
    }
}

最后是ViewCode :(强类型并接收ViewModel)

<asp:Content ID="Content2" ContentPlaceHolderID="JsContent" runat="server">
   <script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
   <script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
   <script src="<%: Url.Content("~/Scripts/question-views.js") %>" type="text/javascript"></script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<h2 class="path"><%= ViewRes.Question.Create.PathCreate %></h2>

    <% using (Html.BeginForm()) { %>
        <%: Html.ValidationSummary(true) %>
            <div>
               <%: ViewRes.Question.Create.DropDownQuestionnaires %>
               <%: Html.DropDownList("Questionnaire_Id", Model.questionnairesList, "--Select--")%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.question.Category_Id)%>
            </div>            
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.question.Category_Id, Model.categoriesList, "--Select--")%>
                <%: Html.ValidationMessageFor(model => model.question.Category_Id)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.question.QuestionType_Id)%>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.question.QuestionType_Id, Model.questionsTypeList, "--Select--")%>
                <%: Html.ValidationMessageFor(model => model.question.QuestionType_Id)%>
            </div>

            <div class="editor-field">
                <%: Html.EditorFor(model => model.question.Text)%>
            </div>            

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

            <p>
                <input type="submit" value="<%: ViewRes.Shared.CreateButton %>" />
            </p>
    <% } %>

</asp:Content>

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您是否尝试过将您的属性设为可空? public int? Category_Id { get; set; }

答案 1 :(得分:0)

您是否在布局中插入了核心Jquery脚本?

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>

您还可以使用Firebug查看验证期间是否出现脚本错误。

答案 2 :(得分:0)

查看生成的HTML。对于将进行客户端验证的所有字段,您将看到类似

的内容
<input class="text-box single-line" data-val="true" data-val-required="The Text field is required." id="Text" name="Text" type="text" value="blah-blah" /&gt;
<span class="field-validation-valid" data-valmsg-for="Text" data-valmsg-replace="true"></span>

然后更容易进行故障排除:问题是没有生成验证代码,或者验证没有发生,或者(如您的示例中的文本字段,验证 发生) ,但你没有Html.ValidationMessage(),所以错误永远不会出现......