I am trying to validate user input on login page.
I have my User mode
with:
- string UserName
- string Password
I decided to extend my DB model with partial class:
[MetadataType(typeof(User))]
public partial class User : IUser
{
}
public interface IUser
{
[Required(ErrorMessage = "Login is required")]
string UserName { get; set; }
[Required(ErrorMessage = "Password is required")]
string Password { get; set; }
}
And my view
:
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.UserName, new { size = 30, @placeholder = "Login",
@class = "credential" })
@Html.ValidationMessageFor(model => model.UserName)
@Html.PasswordFor(model => model.Password, new { size = 30, @placeholder =
"Password", @class = "credential" })
@Html.ValidationMessageFor(model => model.Password)
}
Instead of client validation, it makes postback and crashes on null values in model.
Following this question I added
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
to the top of view, made sure that scripts are loaded:
<script src="@Url.Content("~/Scripts/jquery-1.8.3.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
What am I missing?
Model.Designer.cs
and than does not pass throughModelState.IsValid
– makambi 19 mins ago