Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda