开发者

ASP.NET MVC3 Client Side Validation Help

开发者 https://www.devze.com 2023-03-18 07:43 出处:网络
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

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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消