Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC ValidationMessageFor not working properly

I have the following in my view:

     <fieldset>
        <legend>User Registration</legend>            
        <div class="editor-label">
            @Html.LabelFor(m => m.UsrName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.UsrName)
            @Html.ValidationMessageFor(m => m.UsrName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.Pwd)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.Pwd)
            @Html.ValidationMessageFor(m => m.Pwd)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.ReEnterPwd)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.ReEnterPwd)
            @Html.ValidationMessageFor(m => m.ReEnterPwd)
        </div>            
       <fieldset>
         <legend>Location</legend>
           <span id="locationDiv"> 
           @Html.RadioButtonFor(m => m.Location, "Loc1")  @Html.Label("Loc1")
           &nbsp;&nbsp; 
           </span>
           @Html.RadioButtonFor(m => m.Location, "Loc2") @Html.Label("Loc2")               
           @Html.ValidationMessageFor(m => m.Location)
        </fieldset>         

         <fieldset>
         <legend>Role</legend>
            @Html.RadioButtonFor(m => m.Role, "User") @Html.Label("User")
           &nbsp;&nbsp; @Html.RadioButtonFor(m => m.Role, "Admin") @Html.Label("Admin")
            @Html.ValidationMessageFor(m => m.Role)
         </fieldset>  

        <p>
            <input type="submit" value="Register User" />
        </p> 
    </fieldset>

Even if I don't have all the fields filled, it still goes to the controller even though they are all required. I thought

    @Html.ValidationMessageFor

was supposed to prevent that.

    [Required]
    public string Location { get; set; }

    [Required]
    public string Role { get; set; }

    [Required]
    [Display(Name = "User Name")]
    public string UsrName { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Must have a minimum length of 5.")]
    public string Pwd { get; set; }

    [Required]
    [Display(Name = "Re-enter Password")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Must have a minimum length of 5.")]
    [Compare("Pwd", ErrorMessage = "The password and re-entered password do not match.")]
    public string ReEnterPwd { get; set; }
like image 463
Nate Pet Avatar asked Jan 23 '13 20:01

Nate Pet


2 Answers

You must include the following scripts in the view:

<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>
like image 103
Bahman Avatar answered Sep 30 '22 18:09

Bahman


I had to include the JQuery Validation bundle at the bottom of my view in the scripts section.

I noticed this exists in all the baked in views for login and authentication but needs to be manually added to your custom views.

Example

    @section scripts{
    @Scripts.Render("~/bundles/jqueryval")
}
like image 27
rob23 Avatar answered Sep 30 '22 18:09

rob23