Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
How to get access to whole razor object of chosen element of drop down list at cshtml file through jQuery?

Sorry, I guess question's title little bit confused, I will try to explain properly down below.
I have a model User:

public class User
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
}

And I created couple objects of this class at the controller, e.g.:

|  Id  | Name  | Age |
|------|-------|-----|
| 1111 | John  | 25  |
| 2222 | Sara  | 21  |
| 3333 | Pavel | 34  |

In my html page I have a simple bootstrap drop down list where I can choose from all the users desirable one, like "John", "Sara" or "Pavel". At this dropdown list only shows names of the user, no any other information. For this, I sended list with all the users to the View through controller's action via ViewBag, like ViewBag.Users. And then used it in cshtml file like this:

<select id="inputUsers" class="form-control custom-select" required>
    @{
         foreach (var user in ViewBag.Users)
         {
             <option>@user.Name</option>
         }
    }
</select>

It shows correct, but after choosing desire name I want to use user's other properties at my jQuery script too. For example, if I picked "Sara", I want to use her other info too, like Id(2222) and her age(21) in jQuery script.

In the end I want to use something like this script:

$('#inputUsers').change(function () {
    var userName = $('#inputUsers').val();

    //Something like this. But i don't know how to get access to userAge and userId ;(
    //alert('Hello' + userName + '! Your age is' + userAge + '. And your Id is' + userId) 
});

How can I get access to whole user object with picked name?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda