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?