Return function Boolean via ajax

Asked

Viewed 447 times

1

I have the following ajax call on my page:

$.ajax({
    url: 'EquipamentoTemControleFuncionamento',
    data: { contratocod: contratocod, numeroserie: numerodeserie },
    type: 'POST',
    dataType: 'JSON',
    success: function(retorno) {
        alert('success:' + retorno);
    },
    error: function() {
        alert('error');
    }
});

And the following method in my controller that calls another model method that returns a bool:

public JsonResult EquipamentoTemControleFuncionamento(string contratocod, string numeroserie)
{
    ControleFuncionamentoModel cfm = new ControleFuncionamentoModel();
    return Json(cfm.EquipamentoTemControleFuncionamento(contratocod, numeroserie));
}

I put a breakpoint in the controller’s method, but it’s not stopping. I want to return the Boolean of the Equipment method?

  • Open the browser console (F12) and see if there is an error in the Ajax call. Also open the link in the browser, and see if the controller is working as expected.

  • No error on Ajax call

2 answers

1


Solved. The controller method was the same:

public JsonResult EquipamentoTemControleFuncionamento(string contratocod, string numeroserie)
{
    ControleFuncionamentoModel cfm = new ControleFuncionamentoModel();
    return Json(cfm.EquipamentoTemControleFuncionamento(contratocod, numeroserie));
}

The ajax call, I changed the url call, calling now via @Url.Action:

$.ajax({
     url: '@Url.Action("EquipamentoTemControleFuncionamento")',
     data: { contratocod: contratocod, numeroserie: numerodeserie },
     type: 'POST',
     dataType: 'JSON',
     success: function(retorno) {
            alert('success:' + retorno);
     },
     error: function(retorno) {
            alert('error: '+ retorno);
     }
});

-1

The problem is in your URL, this URL would not be valid in any scenario I can imagine, you should put the URL access to this ACTION in the controller.

Here is an example of ajax with MVC

https://dotnetfiddle.net/zXCm87

Example:

public class Home: Controller
{
    [HttpPost]
    public JsonResult EquipamentoTemControleFuncionamento(string contratocod, string numeroserie)
    {
        ControleFuncionamentoModel cfm = new ControleFuncionamentoModel();
        return Json(cfm.EquipamentoTemControleFuncionamento(contratocod, numeroserie));
    }
}

But Ajax will be serious like this:

$.ajax({
    url: '@Url.Action("EquipamentoTemControleFuncionamento")',
    data: { contratocod: contratocod, numeroserie: numerodeserie },
    type: 'POST',
    dataType: 'JSON',
    success: function(retorno) {
        alert('success:' + retorno);
    },
    error: function() {
        alert('error');
    }
});

or so (if the flame is inside a Razor file):

$.ajax({
    url: '@Url.Action("EquipamentoTemControleFuncionamento", "Home")',
    data: { contratocod: contratocod, numeroserie: numerodeserie },
    type: 'POST',
    dataType: 'JSON',
    success: function(retorno) {
        alert('success:' + retorno);
    },
    error: function() {
        alert('error');
    }
});

Browser other questions tagged

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