i have the following razor of a form
@using (Ajax.BeginForm(new AjaxOptions { }))
{
<div id="frm" style="visibility:hidden">
<table>
<thead>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.LabelFor(x => x.emp.FirstName, new { @class = "form-control" })</td>
<td>@Html.TextBoxFor(x => x.emp.FirstName, new { @class = "form-control" })</td>
<td>@Html.ValidationMessageFor(x => x.emp.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.emp.LastName, new { @class = "form-control" })</td>
<td>@Html.TextBoxFor(x => x.emp.LastName, new { @class = "form-control" })</td>
<td>@Html.ValidationMessageFor(x => x.emp.LastName)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input type="submit" value="Save" class="btn btn-success" id="save"/>
<input type="submit" value="close" class="btn btn-success" id="close"/>
</td>
</tr>
</tbody>
</table>
</div>
}
when i click the close
button the forms hides and the validation should get cleared. But validation clearing part does not work... How do i clear the validations by clicking close
button?
UPDATE: here is my complete code
@model EmployeesTest.Models.EmployeeVM
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<input type="submit" name="name" value="New Entry" id="new"/>
@using (Ajax.BeginForm(new AjaxOptions { }))
{
<div id="frm" style="visibility:hidden">
<table>
<thead>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.LabelFor(x => x.emp.FirstName, new { @class = "form-control"})</td>
<td>@Html.TextBoxFor(x => x.emp.FirstName, new { @class = "form-control", id="firstName" })</td>
<td>@Html.ValidationMessageFor(x => x.emp.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.emp.LastName, new { @class = "form-control" })</td>
<td>@Html.TextBoxFor(x => x.emp.LastName, new { @class = "form-control", id="lastName" })</td>
<td>@Html.ValidationMessageFor(x => x.emp.LastName)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input type="button" value="Save" class="btn btn-success" id="save"/>
<input type="button" value="close" class="btn btn-success" id="close"/>
</td>
</tr>
</tbody>
</table>
</div>
}
@section Scripts {
<script>
$(document).ready(function () {
$('#new').load(x, function (y) {
var form = $('#frm');
form.data('validator', null);
$.validator.unobtrusive.parse(form);
});
$('#new').click(function (evt) {
//evt.preventDefault();
$('#frm').css('visibility', 'visible');
});
$('#save').click(function (evt) {
//evt.preventDefault();
//$('#frm').css('visibility', 'visible');
});
$('#close').click(function (evt) {
//evt.preventDefault();
resetValidations();
$('#frm').css('visibility', 'hidden');
});
function resetValidations() {
//var validator = $('#frm').validate();
//$('#frm').find('.field-validation-error span').each(function () {
// validator.settings.success($(this));
//});
//validator.resetForm();
$('#firstName').addClass('input-validation-valid');
$('#firstName').removeClass('input-validation-error');
$('#lastName').addClass('input-validation-valid');
$('#lastName').removeClass('input-validation-error');
}
});
</script>
}
Here is the Model class EmployeeModel
public class EmployeeModel
{
public int EmpID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
VM
public class EmployeeVM
{
public string DepartmentID { get; set; }
public List<SelectListItem> DepartmentColl { get; set; }
public EmployeeModel emp { get; set; }
public EmployeeVM()
{
emp = new EmployeeModel();
}
}