Viewbag, passing controller value to page razor

-1

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.

    
asked by anonymous 01.06.2018 / 15:50

1 answer

0

How about trying to use the following:

var viewDataVariavel = ViewBag.idHorario;

EDIT: I saw that you updated with JS, but I still have a question (excuse),

 $.ajax({
    ...       
    , success: function (data) {
        if (data.resultado > 0) {
            //console.log(data.resultado);
            ListarItens(idHorario);
        }
    }
});

Could you update with the function ListarItens(idHorario) statement? Thank you.

EDIT 2: I think I realize what you want to do; do so: on the C # side, in Task<ActionResult> SalvarItens(...) , instead of

return new JsonResult(new { Resultado = item.Id });

switch to

return new JsonResult(new { horarioId = HorarioId });

And on the JS side, in function SalvarItens() , exchange

$.ajax({
    ...
    , success: function (data) {
        if (data.resultado > 0) {
            //console.log(data.resultado);
            ListarItens(idHorario);
        }
    }
});

for

$.ajax({
    ...
    , success: function (data) {
        if (data.horarioId) {
            ListarItens(data.horarioId);
        }
    }
});

Be aware that variable names and object properties are case-sensitive in JS!

EDIT 3: in public async Task<ActionResult> SalvarItens(Horarios h, ...) , remove Horarios h :

public async Task<ActionResult> SalvarItens(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;
        ...
}
    
01.06.2018 / 15:58