Return boolean function via ajax

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 method, but it is not stopping. I want to return the boolean of the MethodControlFoundation method via ajax. What should I do?

    
asked by anonymous 09.09.2016 / 19:57

2 answers

1

Solved. The controller method stayed the same:

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

The so called ajax, I changed the call of the url, 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);
     }
});
    
09.09.2016 / 20:52
-1

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

Here is an example of ajax with MVC

link

Example:

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

Ajax would look 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');
    }
});
    
09.09.2016 / 20:20