7

I am trying to get client validation working in MVC3 using data annotations. I have looked at similar posts including this MVC3 Client side validation not working for the answer.

I'm using an EF data model. I created a partial class like this for my validations.

[MetadataType(typeof(Post_Validation))]
public partial class Post
{

}

public class Post_Validation
{
    [Required(ErrorMessage = "Title is required")]
    [StringLength(5, ErrorMessage = "Title may not be longer than 5 characters")]
    public string Title { get; set; }

    [Required(ErrorMessage = "Text is required")]
    [DataType(DataType.MultilineText)]
    public string Text { get; set; }

    [Required(ErrorMessage = "Publish Date is required")]
    [DataType(DataType.DateTime)]
    public DateTime PublishDate { get; set; }
}

My cshtml page includes the following.

<h2>Create</h2>
<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>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Post</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Text)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Text)
            @Html.ValidationMessageFor(model => model.Text)
        </div>
}

Web Config:

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

Layout:

<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>

So, the Multiline Text annotation works and creates a text area. But none of the validations work client side. I don't know what i might be missing. Any ideas?? i can post more information if needed. Thanks!

1
  • Hi, this might not be active anymore, but i just wanna ask how you solve this? I am facing the exactly same problem here, but mine is cant get the validation for text field..any idea?? Thanks... Commented Oct 16, 2011 at 5:20

2 Answers 2

5

1.) Try adding the following into the view which you are validating

HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

I did not find it necessary to modify Web.config, so you may remove

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

Good luck! Also, try putting in a known bad value to see if client side validation is triggered, the required annotation does not seem to be enough to display a message for a blank input.

2.) Put below scripts in your view, it should work.

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Sign up to request clarification or add additional context in comments.

1 Comment

This does work, BUT my problem was that i had the appsettings in the Views\web.config, not the root\web.config. I'm not sure how they got mixed up, as the default project puts them in the root configuration, but it threw me for a loop.
-3

Try to declare using public object :

public class Post_Validation
{
    [Required(ErrorMessage = "Title is required")]
    [StringLength(5, ErrorMessage = "Title may not be longer than 5 characters")]
    public object Title { get; set; }

    [Required(ErrorMessage = "Text is required")]
    [DataType(DataType.MultilineText)]
    public object Text { get; set; }

    [Required(ErrorMessage = "Publish Date is required")]
    [DataType(DataType.DateTime)]
    public object PublishDate { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.