4

I have a simple strongly-typed view.

@model GoldForGold.Models.LogonModel
@{
    ViewBag.Title = "Logins";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Logins
@using (Html.BeginForm()) {

Account Information
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { id = "txtUserName" })
@Html.ValidationMessageFor(m => m.UserName)
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { id = "txtPassword" })
@Html.ValidationMessageFor(m => m.Password)
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)

<input type="submit" value="Log On" onclick="getcredentials()" />
}

Model code is here.

public class LogonModel
{
    [Required(ErrorMessage="please enter username")]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

I see nothing is happening even when I do not enter username and password.

tomRedox
  • 24,242
  • 20
  • 108
  • 147
user2465036
  • 341
  • 1
  • 11
  • 24

7 Answers7

35

Here is the complete solution:

  1. Add the following scripts to your view:

    <script src='@Url.Content("~/Scripts/jquery-1.8.2.js")' type='text/javascript'></script>    
    <script src='@Url.Content("~/Scripts/jquery.validate.js")' type='text/javascript'>         </script>
    <script src='@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")' type='text/javascript'></script>
    
  2. Enable Validation in Web.Config:

    <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    
  3. Add validation element in the view:

    @Html.ValidationMessageFor(model => model.UserName)
    
  4. Add Required attribute to you model`s element.

    [Required(ErrorMessage="please enter username")]
    public string UserName { get; set; }
    
Vahagn Nahapetyan
  • 1,307
  • 13
  • 22
9

For client side validation you need jquery and jquery validation.

after that client side validation must be enabled in config:

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

for server side validation you can check your validation state with:

ModelState.IsValid;
MRB
  • 3,687
  • 3
  • 31
  • 41
  • I had these set, but prompted me to review my includes. did not have query.validate.min.js or jquery.validate.unobtrusive.min.js included. rookie mistake. – ransems Aug 14 '20 at 15:03
6

For the record, you can include the following code at the end of your view, to make sure that the [Required] attribute pops up in the user interface:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
marcus
  • 313
  • 7
  • 14
6

One other thing to be wary of with [Required] is when using it on an integer field, not just when storing a numeric value, but also, say, when storing the id of the selected value of a dropdown/select list:

[Required] won't work in that situation because an integer property can never be null and so always has a default value of zero. [Required] is checking that the property has a value, and zero is considered a value, so the check will pass.

In that case either declare the property as a nullable integer instead:

    [Required]
    public int? LanguageId { get; set; } // note the int is now nullable

or alternatively use the Range attribute to stop zero being an acceptable value:

    [Range(1, int.MaxValue)]
    public int LanguageId { get; set; }
tomRedox
  • 24,242
  • 20
  • 108
  • 147
2

My fix for my problem was to use this line of code in the model class.

using System.ComponentModel.DataAnnotations;

be aware that there is another namespace (Microsoft.Build.Framework) that also supports [Required] annotation.

Dharman
  • 26,923
  • 21
  • 73
  • 125
Ike
  • 1,124
  • 12
  • 17
0

In my case this happened because I didn't install a package Microsoft.jQuery.Unobstrusive.Validation

After this was installed and ran the WebApp it worked.

Example person
  • 2,832
  • 3
  • 12
  • 39
ardodiaz
  • 61
  • 1
  • 5
-3
  • Steps for solution:

1- Add the scripts to view:

<script src='@Url.Content("~/Scripts/jquery-1.8.2.js")' type='text/javascript'></script>       
<script src='@Url.Content("~/Scripts/jquery.validate.js")' type='text/javascript'>         
</script>
<script src='@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")' type='text/javascript'></script>

2- Enable Validation in Web.Config:

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

3- Add validation html helper in the view:

@Html.ValidationMessageFor(model => model.UserName)

4- Add Required attribute to your model`s element:

[Required(ErrorMessage="please enter username")]
public string UserName { get; set; }
Jehad87
  • 41
  • 3