AJAX function does not pass the result to another function

0

I have this function in AJAX:

function SalvarHorario() {


    //NomeHorario
    var nome = $("#Nome").val();

    var token = $('input[name="__RequestVerificationToken"]').val();
    var tokenadr = $('form[action="/Horario/Create"] input[name="__RequestVerificationToken"]').val();
    var headers = {};
    var headersadr = {};
    headers['__RequestVerificationToken'] = token;
    headersadr['__RequestVerificationToken'] = tokenadr;

    //Gravar
    var url = "/Horario/Create";

    $.ajax({
        url: url
        , type: 'POST'
        , headers: headersadr
        , data: { Id: 0, Nome: nome, __RequestVerificationToken: token }
        , success: function (data) {
            if (data.Resultado > 0) {
            ListarItens(data.Resultado);
            //    alert(data.Resultado);
            }
        }
    });
}

The result is correct, it is getting the right ID, but it does not go to the ListItems function, I already did this in other projects, and it worked fine, it understands that it is not greater than 0, takes the last ID entered correctly, how can I proceed? I've tried it in many ways, and none of it passes the correct ID.

This is the list items:

function ListarItens(idHorario) {

    var url = "/HorariosItens/ListarItens";

    $.ajax({
        url: url
        , type: "GET"
        , data: { id: idHorario }
        , datatype: "html"
        , success: function (data) {
            var divItens = $("#divItens");
            divItens.empty();
            divItens.show();
            divItens.html(data);
            $("#idItem").val("0");
        }
    });

}

Where do I need this function:

 public ActionResult ListarItens(int id)
        {
            var lista = _context.HorariosItens.Where(m => m.Horarios.Id == id);
            ViewBag.HorarioId = id;
            ViewBag.ItemId = 0;
            return PartialView(lista);
        }

I believe that the problem is in the date.Result, because it is not happening, if I put an alert, it returns me undefined, however it is included successfully, so long as it passes from success: function (data).

Create Time:

 if (ModelState.IsValid)
            {
                _context.Horarios.Add(h);
                _context.SaveChanges();
            }

            return new JsonResult(new { Resultado = h.Id });
    
asked by anonymous 29.05.2018 / 21:21

1 answer

1

The problem occurred because by default the json returns in the .NET Core are in camel casing , and the first letter is lowercase.

One way to break this is in Startup.cs "inform" that you want json to be as written in the variable. The code for this would be:

services.AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    }); 

Another solution, as commented, is to expect returns to be on camel casing , that is, Return will Return

You can see more about camel casing on this link

    
29.05.2018 / 22:01