Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Access ViewModel integer list with javascript

I’m working on ASP.Net MVC Core application and I have a view model with a list of integers:

  public List<int?> SelectedIds { get; set; } = new List<int?>();

Now I want to access this property via javascript in order:

@model Project.ViewModels.Test.MyViewModel

  $(function() {
                 var test = @Model.SelectedIds;
   });

But in the Chrome console this is throwing an error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 var test = System.Collections.Generic.List`1[System.Nullable`1[System.Int32]];

What am I doing wrong?

>Solution :

@model Project.ViewModels.Test.MyViewModel

  $(function() {
                 var test = @Html.Raw(Json.Serialize(Model.SelectedIds));
   });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading