Mah
Mah

Reputation: 77

How can I access list of objects in jquery?

I have this in my viewModel.

 public IEnumerable<TeamDto> Teams { get; set; }

And, in my view:

 <input  name="Teams" id="Teams" [email protected] />

How do I access the team list in jQuery?

 var data2 = $("#Teams")??

Upvotes: 0

Views: 488

Answers (2)

Sujit Patel
Sujit Patel

Reputation: 155

Use the below code on the same cshtml page in end of page.

@{
        IEnumerable<TeamDto> loRes = new IEnumerable<TeamDto>();
        loRes = Model.Teams;
        <script>
        console.log('@loRes.FirstOrDefault().fiedlName');
        </script>
}

Upvotes: 1

Josh Bonnick
Josh Bonnick

Reputation: 2813

You access the value of an input by using the val() method. You can also set the value with val('your value')

var data2 = $("#Teams").val();

Upvotes: 1

Related Questions