I have a partial view containing a user creation form. When I call that form within a main view using @Html.Partial("_CreateUser")
and attempt to submit it empty, none of the Data annotation validations work. However if I return that partial view by itself to the browser, and try it again, validation messages work just fine. Any suggestions would be appreciated. Code is below.
ViewModel
[Required(ErrorMessage = "Please enter a first name.")]
[Range(2,12, ErrorMessage = "First name must be between {1} and {2}
characters in length.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter a last name.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
Partial View Snippet
@using (Html.BeginForm("CreateUser", "OpsManagement"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-3">
<!--- First Name -->
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName,"", new { @class = "text-danger" })
</div>
<div class="col-md-3">
<!--- Last Name -->
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Main View
<div id="newUserPartialView" class="collapse">
@Html.Partial("_CreateUser")
</div>