Get Hidden value . net core

Asked

Viewed 129 times

0

Good morning I have this Hidden that gets the value correctly:

<input type="hidden" id="idHorario"/>

But I need to get the Hidden in the code, how can I proceed?

 HorariosItens = await _context.HorariosItens
                 .Include(a => a.Horarios)
                 .Where(a => a.HorarioId == ["hidden aqui"] )
                 .ToListAsync();

I am unable to pass the value of Hidden to the condition.

Edit: I have this function where you send the Horarioid to save to the Horarioitem/Create page

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

And this one to save:

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,
               
            };
            //try
            //{
            ViewData["hor"] = HorarioId;
            _context.HorariosItens.Add(item);
                _context.SaveChanges();

            //HorariosItens = await _context.HorariosItens
            //   .Include(a => a.Horarios).Where(a => a.HorarioId == HorarioId).ToListAsync();

            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

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

And then on this, where I should list the items schedules, but I’m not able to work passing the id:

 public IList<HorariosItens> HorariosItens { get; set; }
        public async Task<IActionResult> OnGetAsync()
        {
            try
            {
                HorariosItens = await _context.HorariosItens
                  .Include(a => a.Horarios).Where(a => a.HorarioId == int.Parse(ViewData["hor"].ToString())).ToListAsync();
            }
            catch
            {
                HorariosItens = await _context.HorariosItens
                   .Include(a => a.Horarios).Where(a => a.HorarioId == 0).ToListAsync();
            }
            //ViewData["HorarioId"] = new SelectList(_context.Horarios, "Id", "Nome");
            return Page();
        }

I even put a Try catch, but it smp falls in the catch.

  • <input type="hidden" id="idHorario"/> it should be like this <input type="hidden" id="idHorario" name="idHorario"/> because what counts for programming is the name of input!!!

  • I still can’t do it.

  • What did you put in the ??? That’s more or less the way: GetAprovacaoEmendaById(int idHorario) !!! to rescue!

  • @Virgilionovic edited the question so you can understand. thanks.

  • Here , data: { id: idHorario } trial ,data: { id: document.getElementById('idHorario').value }

  • But then passes the normal value, I need to pick to bring the items, the idHorario there, It is filled.

Show 1 more comment

1 answer

0

You must pass the Hidden pro controller input value and then move to the query method:

[HttpGet]
public JsonResult GetAprovacaoEmendaById(tipododado valordoInput)
{
  HorariosItens horarios = SeuMetodoDeConsulta(valordoInput);  
}

And in your query method receive this value:

public HorariosItens(tipododado valordoInput)
{
  HorariosItens = await _context.HorariosItens
             .Include(a => a.Horarios).Where(a => a.HorarioId == valordoInput ).ToListAsync();
}

I hope I’ve helped.

  • Hidden already has the value, I just need to use it. in comparison, not being able to make it work.

  • The value of the Hidden input is on your front or back end?

  • I use Pagerazor, by javascript I fill the Hidden that is in html create, and within the create control I need to use this Hidden.

  • You’re getting this value from the controller?

  • Yes, I edited the question so you can understand. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.