Communication between View and Controller via a script

Asked

Viewed 57 times

1

I’m trying to call an Action through . ajax that runs inside a Modal button but apparently the url is not processed. My View is 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>
        <script src="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 on the Save button inside Modal nothing happens. I already tried to put the url: '~/Coollab/Save' and also use the callPostBack method but still can’t open the Save Action. I need to save even when the registered value is outside the validation parameter. Can anyone see why the button does not direct to Action?

  • With that Validateantiforgerytoken you will not be able to access via Ajax.

  • However, I don’t understand what you want to happen, are you trying to get a form to be posted? or call this action by ajax without passing anything? because this Coolant class will not be populated unless you submit a form, or build a JSON with the properties.

  • But without Validateantiforgerytoken gives the error: Server Error in Application '/'. The anti-counterfeiting form field "__Requestverificationtoken" is not present.

  • So Ana, the explanation is a little vague. -( I don’t understand what you want to do, there is a form on the screen with this Token?

  • Thank you Ana. So this Beginform will not process by Ajax, you need to use Ajax.Beginform takes a look at this article, it teaches you how to use https://www.aspsnippets.com/Articles/ASPNet-MVC-AjaxBeginForm-Tutorial-with-example.aspx

  • I just updated my form in reply. I am placing the data through View Create and when I click on the save button I validate the data in the Controller through a table of References previously registered in the database. If something is not ok, I signal to the View via a Viewbag.Message and the View opens the Modal with the confirmation message. The problem is that I need to be able to save in the database even when the data is not ok. That’s why I want to call Save Action by clicking the Save Modal button. I hope I was able to explain myself, rs.

  • I’ll look! Thank you very much! :)

  • You are welcome, so what you can do is take this onclick and leave the submit form alone. but the modal code needs to be inside the form, or else put the attribute form=idFormulario on the save button, so he knows which form he belongs to, then put an id in your form.

  • url:'@Url.Action("Salvar", "ResfriadorLAB")' It’s not the spaces?

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.