Validate fields on the Modal Bootstrap 4 screen with Asp.Net MVC

0

Hello,

I am doing a CRUD in an Asp.Net MVC 5 application using modal screens bootstrap 4. My template class has required fields using data annotation. But when one of the required fields is not informed, I can not display the review on the modal screen. Everything else works fine, but I can not see the return validation coming from the server.

My controller:

        
        public ActionResult Create()
        {
            return PartialView();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Nome")] Comarca comarca)
        {
            if (ModelState.IsValid)
            {
                db.Comarcas.Add(comarca);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(comarca);
        }

My main view, I call my modal screen like this:

<div class="modal fade" id="modalCreate" tabindex="-1" role="dialog">

</div>

Screen calling script:

            //novo
            $("#btnNovo").click(function () {
                $("#modalCreate").load('@Url.Action("Create","Comarcas")',
                    function () {
                        $("#modalCreate").modal("show");
                    }
                );
            });

My modal screen:

@model TelaModal.Models.Comarca

@{
    Layout = null;
    ViewBag.Title = "Create";
}

<div class="modal-dialog" role="document">
    <div class="modal-content">
        @using (Html.BeginForm(new { id = "formCreate" }))
        {
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">Novo produto</h4>
            </div>

            @Html.AntiForgeryToken()

            <div class="modal-body">
                <div class="form-horizontal">
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })


                    <div class="form-group">
                        @Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <input type="submit" value="Salvar" class="btn btn-default"/>
                <input type="button" value="Cancelar" class="btn btn-default" data-dismiss="modal" />
            </div>
        }
    </div>
</div>

Thank you all!

    
asked by anonymous 05.11.2018 / 01:18

0 answers