Communication between View and Controller through a script

1

I'm trying to call an Action through the .ajax that runs within a button of a Modal but apparently the url is not processed. My View looks like this:

@using (Html.BeginForm())
{
<div class="form-horizontal">
  <h2>Resfriador</h2>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(model => model.Data_Coleta, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-2">
                        @Html.EditorFor(model => model.Data_Coleta, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Data_Coleta, "", new { @class = "text-danger" })
                    </div>
                </div>

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

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

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

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

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

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

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

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

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

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

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

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



                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Salvar" class="btn btn-default " data-toggle="modal" data-target="#myModal" />
                    </div>
                </div>
            </div>
        }

        @if (ViewBag.Message == "Negado") 
            {
                <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-body">
                                <h4>ATENÇÃO!</h4>
                                <p id="demo">
                                    Valor fora do parâmetro. Deseja salvar?
                                </p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Fechar</button>
                                <button type="submit" class="btn btn-success btnSalvar" id="Salvar" onclick="myFunction(this.id)" >Salvar</button>
                            </div>
                        </div>
                    </div>
                </div>

            }

Script:

   @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

        <script>
            $(document).ready(function () {

                $('#myModal').modal('show');

            });
        </script>

        <script>
            function myFunction(value)
            {
                $.ajax({
                        type: 'POST'
                        url:' @Url.Action("Salvar", "ResfriadorLAB") ',
                    })
             };
       </script>
}

Action Save:

[HttpPost]
[ValidateAntiForgeryToken]
        public ActionResult Salvar(ResfriadorLAB resfriadorLAB)
        {
            var objlogin = db.Login.OrderByDescending(p => p.LoginId).FirstOrDefault();
            var objref = db.ReferenciasLABs.OrderByDescending(p => p.ReferenciasLABId).FirstOrDefault();

            resfriadorLAB.Data_Cadastro = DateTime.Today;
            resfriadorLAB.Hora_Cadastro = DateTime.Now;
            resfriadorLAB.LoginId = objlogin.UsuarioId;
            resfriadorLAB.ReferenciasLABId = objref.ReferenciasLABId;
            if (ModelState.IsValid)
            {
                db.ResfriadorLABs.Add(resfriadorLAB);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.LoginId = new SelectList(db.Login, "LoginId", "LoginId", resfriadorLAB.LoginId);
            ViewBag.ReferenciasLABId = new SelectList(db.ReferenciasLABs, "ReferenciasLABId", "ReferenciasLABId", resfriadorLAB.ReferenciasLABId);
            return View(resfriadorLAB);
        }

When I click the Save button from within the Modal nothing happens. I have already tried to put the url: '~ / CoolerLAB / Save' and also use the callPostBack method but I still can not open the Save Action. I need to save even when the value entered is outside the validation parameter. Can anyone see why the button does not direct to Action?

    
asked by anonymous 14.12.2017 / 18:43

0 answers