-1
I have this Ajax to be able to take the data and pass to the function Save items that is in the Schedulesitenscontroller, that even 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 });
}
On the create page, in this method, I try to get the value of idHorario
, but it is always coming in null
.
And this is where he’s like "Load" on the 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 several ways, but none comes with value, I need to click with the value. I have the pageRazor Create de Horario item, on it I create, and I have the table, which in case should update, after including the time item. I don’t know if you can understand, is that the Pagerazor I can not create the view design, the same when MVC is used without Core. So I’m doing it this way.
o Listaritens, is this function 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 include 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 the a.HorarioID == "Numerodoidaqui"
without being the variable, it loads, I need to pass the variable in this function.
Mariana, do not use the Snippet for non-html/JS/CSS code in a full example. For all others, use the button
{}
of the editor.– Woss
Why do you want to get the idHorario value in the Ongetasync method? The purpose is not to access the Viewbag in the View?
– perozzo
I use pageRazor @Perozzo, his controller is very confusing to tell you the truth, I use json to save in the bank, and in the same ajax I call the save items, and then I need after saving is loaded the table, which is on the same page.
– Mariana
You are returning Result = item. Id in Save Items, should not be item.Horarioid?
– perozzo
No, because in the time items I load all the time items Where idHorario = item.HorarioId. Are several items for a time.
– Mariana
Viewdata and Viewbag serve to send Controller information to View, not the other way around
– Leandro Angelo
I just need the table to be loaded with the time id, the Pagerazor is very complicated, I’m not able to do it that way, either by ajax, on the same page.
– Mariana
I updated my answer, could you check? Thank you
– Marcelo Shiniti Uchimura
I updated again; now, I think it’s definitive! :)
– Marcelo Shiniti Uchimura
I added another hint to my reply!
– Marcelo Shiniti Uchimura