Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
Have you any error in web browser? – Shahrooz Jefri ㇱ 21 mins ago
Dont have any errors, It crashes in Model.Designer.cs and than does not pass through ModelState.IsValid – makambi 19 mins ago
1  
@shahrooz-jefri, thanks I will take a look – makambi 13 mins ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.