Pass controller(action) information to a jquery function and vice versa

Asked

Viewed 32 times

1

A jquery function will call a modal screen when changing a Dropdownlist. When I make the change, the value of id should be sent to Action there is then made a calculation and the result is sent to the jquery. Soon that jquery, takes this result and checks, if the value is 0(zero), then the Modal is displayed, otherwise no. I’m trying to mount this jquery, just the beginning of it, but I don’t know how to pick up the call back of function jquery. All in this function are attempts, they are not complete or correct, I imagine.

$(document).ready(function () {
        $('#faturarParaDrop').change(function () {
            var $div = $('#modalPartial');
            $.ajax({
                url: '',
                type: 'GET',
                success: function (dados) {
                    alert(dados);
                    $div.html(dados);
                    alert(2);
                },
                error: function (erro) {
                }
            })
        });
    });

That’s the Controller which will calculate and receive the jquery parameter. I have not yet put the parameter, because I have doubts about how to do it and the var qry to return 0 or another value and jquery if 0 display is not following the flow.

[HttpGet]
        public ActionResult FaturarPara(int id)
        {
            ViewBag.Order = _orderService.GetById(CurrentReseller.Id, CurrentCustomer.CustomerGuid, id);

            var options = _orderService.ListarFaturarParaOptions(CurrentReseller.Id, CurrentCustomer.CustomerGuid, id);

            var customer = _customerService.ListCustomerByReseller(CurrentReseller.Id);

            var qry = customer.Where(x => x.Type == CustomerTypeRequest.Customer);

            return View(options);
        }

How I pass and take value in one jquery for call back and controller(action)?

EDIT1

By changing jquery to this one, I get the dropdownlist ID. With this parameter, I can then query. Now, as a return to jquery the value returned from the query, for me to call or not the modal?

$(document).ready(function () {
        $('#faturarParaDrop').change(function () {
            var $div = $('#modalPartial'); //exibir a modal
            var $idcustomer = $(this).val(); //valor do id da dropdownlist
            $.ajax({
                url: '',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                type: 'POST',
                data: JSON.stringify({ idcustomer: $idcustomer }),
                success: function (dados) {
                    alert(dados);
                    $div.html(dados);
                    alert(2);
                },
                error: function (erro) {
                }
            })
        });
    });

Controller Action looks like this

[HttpPost]
        public ActionResult FaturarPara(OrderFaturarParaOptions model, int idcustomer)
        {
            var order = _orderService.SalvarFaturarPara(CurrentReseller.Id, CurrentCustomer.CustomerGuid, model);
            var customer = _customerService.ListCustomerByReseller(CurrentReseller.Id);

            var qry = customer.Where(x => x.Type == CustomerTypeRequest.Customer && x.CustomerId == model.CustomerId)
                                 .Select(x => x.CompanyName ).FirstOrDefault();

            return RedirectToAction("GetOrderDetail", "SearchOrders", new { id = model.OrderId });
        }
  • And what of this action you want to return to the view, to options?

  • @Leandroangelo, I would like to return only one value, which would be qry, after the query is fully assembled, type qry = 0 or qry = 3.45 and so on. This value is going to shoot or not the Modal

  • Based on Leandro’s answer, I think the post in jquery, for what I want, didn’t see it working. I switched to GET, but the parameter doesn’t arrive in Action

  • The whole point is that I route jquery by passing,controller/action_name and nothing happens. If I try to get another Action inside my controller, it doesn’t roll, it doesn’t work, only that jquery is inside the View XPTO. It has influence?

1 answer

1

If you want to return the rendered html from the view, just use the load method()...

$(document).ready(function () {
    $('#faturarParaDrop').change(function () {
        $('#modalPartial').load('sua_url/?id=id_selecionado');
    });
});
  • A question. This controller has two actions (Get and Post). When the screen goes up, it consumes the GET method. And when the Submit button is pressed, then I consume the Post method. In the case of Dropdownlist change, I should actually use Get or not or whatever?

  • As a rule, it’s simple... if you’re requesting server information is GET, if you’re sending is POST (and its variations PUT, PATCH, DELETE and etc) ...

  • I understood, but in my case, I send a parameter (Dropdownlist Value) and receive the result of a calculation in Action. That understanding is what’s bothering me.

  • My huge difficulty is that with Get or Post, in the function Success I have the data, for example, and in it I get an Html(from the page) and I need to pick up is just the return of a query and this is what I do not know how to do.

  • return from which query?

  • I think if I return View, I will always give this stick. I must change the Action to return a json, correct? The query that is inside the Action

Show 1 more comment

Browser other questions tagged

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