I am calling a view(partial view) via jquery modal, and within that modal, I have a button that does the action of creating a new employee. But I'm not managing to make, if the spaces are not filled or not filled well, the error warnings appear as it happens in the normal view.
Can anyone help me?
My View Index(Employees)
<div class="modal fade" id="myModal3" role="dialog" ata-keyboard="false"
data-backdrop="static">
<div class="modal-dialog modal-lg modal-dialog-centered" role="document">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Criar Funcionário</h4>
</div>
<div class="modal-body" id="create">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary btn-submitcreate">Criar</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
@section Scripts {
<script>
$(document).ready(function () {
<button type="button" class="btn btn-info btn-lg btn-create" data-toggle="modal" data-target="#myModal3">Criar Funcionário</button>
$(".btn-create").on("click", function (e) {
$("#create").load("/Employees/Create/");
var validator = $('#create').validate();
validator.form();
});
$(".btn-submitcreate").on("click", function (e) {
var parentDiv = $('#create');
$.post(parentDiv.find('form').attr('action'),
parentDiv.find('form').serialize(),
function (data) {
location.reload();
});
});
});
</script>
}
My Create View
The following code is my fields in the create view
<div class="col-md-2">
<div class="col-md-12">
@Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-10">
<div class="col-md-12">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="col-md-12">
@Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
@Html.LabelFor(model => model.MaritalStatus, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.MaritalStatus, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MaritalStatus, "", new { @class = "text-danger" })
</div>
</div>
</div>
validate()
on theform
when the DOM loads, not when the submit button is clicked, then apply the validation rules asdata
attributes to the dynamic elements as they're appended to the DOM. Also, use thesubmithandler
property ofvalidate
to send the form using AJAX.