0

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">&times;</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>
3
  • 1
    The simplest way is to call validate() on the form when the DOM loads, not when the submit button is clicked, then apply the validation rules as data attributes to the dynamic elements as they're appended to the DOM. Also, use the submithandler property of validate to send the form using AJAX. Commented Apr 29, 2021 at 16:04
  • I put in the validate code when it loads, I still don't understand how I can apply the validation rules as data attributes to the dynamic elements and use the validate submithandler property to submit the form using AJAX. Commented Apr 29, 2021 at 16:27
  • "I still don't understand how I ... use the validate submithandler property to submit the form using AJAX" - Did you review the documentation, the code examples, or the other SO questions about this plugin?
    – Sparky
    Commented Apr 30, 2021 at 22:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.