I have this Ajax to be able to get the data and move to the function Save items that is in the schedulesItemsController, which works perfectly.
function SalvarItens() {
var idItem = $("#idItem").val();
if (idItem == 0) {
var dataInicio = $("#txtHoraInicio").val();
var dataFim = $("#txtHoraFim").val();
var tipoLimite = $("#txtTipoLimite").val();
var limiteAcessos = $("#txtLimiteAcessos").val();
var cbSeg = $('#cbSeg').prop('checked');
var cbTer = $('#cbTer').prop('checked');
var cbQua = $('#cbQua').prop('checked');
var cbQui = $('#cbQui').prop('checked');
var cbSex = $('#cbSex').prop('checked');
var cbSab = $('#cbSab').prop('checked');
var cbDom = $('#cbDom').prop('checked');
var cbFer = $('#cbFer').prop('checked');
var idHorario = $("#idHorario").val();
var url = "/HorariosItens/SalvarItens";
$.ajax({
url: url
, data: { HoraInicio: dataInicio, HoraFim: dataFim, Seg: cbSeg, Ter: cbTer, Qua: cbQua, Qui: cbQui, Sex: cbSex, Sab: cbSab, Dom: cbDom, Fer: cbFer, Tipolimite: tipoLimite, Limiteacessos: limiteAcessos, HorarioId: idHorario }
, type: "POST"
, datatype: "html"
, success: function (data) {
if (data.resultado > 0) {
//console.log(data.resultado);
ListarItens(idHorario);
}
}
});
}
Here is the function that saves the time item:
public async Task<ActionResult> SalvarItens(Horarios h, string HoraInicio, string HoraFim, bool Seg, bool Ter, bool Qua, bool Qui, bool Sex, bool Sab, bool Dom, bool Fer, int Tipolimite, int Limiteacessos, int HorarioId)
{
h.Id = HorarioId;
var item = new HorariosItens()
{
HoraFim = HoraFim,
HoraInicio = HoraInicio,
Seg = Seg,
Ter = Ter,
Qua = Qua,
Qui = Qui,
Sex = Sex,
Sab = Sab,
Dom = Dom,
Fer = Fer,
Tipolimite = Tipolimite,
Limiteacessos = Limiteacessos,
HorarioId = HorarioId,
};
ViewBag.idHorario = HorarioId;
_context.HorariosItens.Add(item);
_context.SaveChanges();
return new JsonResult(new { Resultado = item.Id });
}
In the create page, in this method, I try to get the value of idHorario
, but it is always coming in null
.
And here is where it does type the "Load" page.
public async Task<IActionResult> OnGetAsync()
{
var viewDataVariavel = ViewData["idHorario"];
if(viewDataVariavel != null)
{
HorariosItens = await _context.HorariosItens
.Include(a => a.Horarios).Where(a => a.HorarioId == int.Parse(viewDataVariavel.ToString())).ToListAsync();
}
else
{
HorariosItens = await _context.HorariosItens
.Include(a => a.Horarios).Where(a => a.HorarioId == 0).ToListAsync();
}
return Page();
}
I have tried in many ways, but none comes with value, I need to load with the value. I have the pageRazor Create of Schedule item, on it I create, and I have the table, which in case it should refresh, after including the time item. I do not know if you can understand, is that PageRazor I can not create the design of the view, same as using MVC without the Core. So I'm doing it that way.
ListItems, this function is in ajax:
function ListarItens(idHorario) {
var url = "/HorarioItem/Create";
$.ajax({
url: url
, type: "GET"
, data: { id: idHorario }
, datatype: "html"
, success: function (data) {
console.log(idHorario);
var divItens = $("#divItens");
divItens.empty();
divItens.show();
divItens.html(data);
$("#idItem").val("0");
$("#idHorario").val(idHorario);
}
});
}
Edit:
Every time I add a new time item, it enters this function:
public async Task<IActionResult> OnGetAsync()
{
HorariosItens = await _context.HorariosItens
.Include(a => a.Horarios).Where(a => a.HorarioId == 0).ToListAsync();
return Page();
}
If I pass a.HorarioID == "Numerodoidaqui"
without being the variable, it loads, I need to pass the variable in this function.