0
votes

I'm using unobtrusive client side validation for my current application but the problem is that it's not validating all fields but only some of them. First I thought that DropDownLists were the ones omitted by the validator, but after changing those for simple TextBoxes, I realized it's not working either. I really don't know what it is. So, I hope you can give me a hand:

I already have on my Web.Config:

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

I have the Metadata for one of the classes that is not working properly:

[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; }
    }
}

Finally the ViewCode:(Strongly-Typed and receives the 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>

Thanks for any help.

3
Code generated for fields not validating properly: code <div class="editor-label"> <label for="question_Category_Id">Category</label> </div> <div class="editor-field"> <select id="question_Category_Id" name="question.Category_Id"><option value="">--Select--</option> <option value=""></option> </select> <span class="field-validation-valid" data-valmsg-for="question.Category_Id" data-valmsg-replace="true"></span> </div> - met.lord
Code generated for fields not validating properly: code <div class="editor-label"> <label for="question_QuestionType_Id">Question Type</label> </div> <div class="editor-field"> <select id="question_QuestionType_Id" name="question.QuestionType_Id"><option value="">--Select--</option> <option value="1">Selection Answers</option> <option value="2">Text Answers</option> </select> <span class="field-validation-valid" data-valmsg-for="question.QuestionType_Id" data-valmsg-replace="true"></span> </div> - met.lord
Looks that both Question and Category are not validating, correct (I thought you said elsewhere that one is validating... Here is what should be generated for select (that is required and numeric): <select name="CourseID" id="CourseID" data-val-required="The CourseID field is required." data-val-number="The field CourseID must be a number." data-val="true" class="input-validation-error"><option value="">Choose...</option> Your Question class is partial... this assumes that you have another file (probably generated by EF) hat has the same values as your metadata class... - Felix
Yes, Question is a partial class since there is another generated with LINQ. But I still don't see any error... I mean, I know the generated code is wrong but I don't see why is that happening. Do you know why? - met.lord

3 Answers

0
votes

Have you tried making your properties nullable? public int? Category_Id { get; set; }

0
votes

Are you inserting core Jquery script in you Layout?

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

you can also use Firebug to see if there is a script error throwing up during validation.

0
votes

Take a look at HTML that is generated. For all fields that will undergo client-side validation you will see something like

<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>

Then it's easier to troubleshoot: is the problem that validation code isn't generated, or that validation doesn't happen, or (as would be in your example with Text field, validation does happen, but you don't have Html.ValidationMessage(), so error never shows up...