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 });