0

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")??

2 Answers 2

1

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>
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

I already tried that but it doesn't work. when I add.val() , it gives me :"System.Collections.Generic.List`1[Rype.Teams.Dto.TeamDto]" I can't see the list
I'm not quite sure what your trying to achieve here. The val function will return the inputs value but you need to consider what format it's in, possibly JSON.parse (or the jQuery equivalent) it to get data JavaScript can use.
Sorry, I read than as an error message not the actual value.... you need to get @Model.Teams to output in json, which I don't know how to do. But I believe that's your issue with accessing the object from javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.