Form model not validating before POST
<p>I'm trying to make a registration page in my web application. However, the validation on my form is not working. For example I have my password minimum length be 4 charterers but if I enter a blank password it still makes the POST request. How do I tell the form that the model is not valid and that they should change their password,email, etc? </p>

<p><strong>AccountController.cs</strong></p>

<pre><code>[HttpPost]
public ActionResult Register(RegisterModel model){
    if (ModelState.IsValid){
        // Insert into database
    }
    // There was an error
    return View(model);
}
</code></pre>

<p>RegisterModel.cs</p>

<pre><code>public class RegisterModel
{

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

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

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 4)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}
</code></pre>

<p>Register.cshtml</p>

<pre><code>@model RegisterModel
    &lt;form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal"&gt;
        &lt;h4&gt;Create a new account.&lt;/h4&gt;
        &lt;hr /&gt;
        &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label asp-for="UserName" class="col-md-2 control-label"&gt;&lt;/label&gt;
            &lt;div class="col-md-10"&gt;
                &lt;input asp-for="UserName" class="form-control" /&gt;
                &lt;span asp-validation-for="UserName" class="text-danger"&gt;&lt;/span&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label asp-for="Email" class="col-md-2 control-label"&gt;&lt;/label&gt;
            &lt;div class="col-md-10"&gt;
                &lt;input asp-for="Email" class="form-control" /&gt;
                &lt;span asp-validation-for="Email" class="text-danger"&gt;&lt;/span&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label asp-for="Password" class="col-md-2 control-label"&gt;&lt;/label&gt;
            &lt;div class="col-md-10"&gt;
                &lt;input asp-for="Password" class="form-control" /&gt;
                &lt;span asp-validation-for="Password" class="text-danger"&gt;&lt;/span&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label asp-for="ConfirmPassword" class="col-md-2 control-label"&gt;&lt;/label&gt;
            &lt;div class="col-md-10"&gt;
                &lt;input asp-for="ConfirmPassword" class="form-control" /&gt;
                &lt;span asp-validation-for="ConfirmPassword" class="text-danger"&gt;&lt;/span&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;div class="col-md-offset-2 col-md-10"&gt;
                &lt;button type="submit" class="btn btn-default"&gt;Register&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/form&gt;
</code></pre>

<p><strong>Soultion</strong>
Since The Register page was under Home controller, and the Register logic was in the Account controller i had to define the the view to load the Register.cshtml page. </p>

<pre><code>[HttpPost]
public ActionResult Register(RegisterModel model){
    if (ModelState.IsValid){
        // Insert into database
    }
    // There was an error
    return View("~/Views/Home/Register.cshtml",model)
}
</code></pre>