how to parse C# generic collection list in typescript












0















This is my .cshtml code,



  @ { var myList = (List<MyViewModel>)ViewBag.MyCollection; }
<input id="myListHidden" type="hidden" data-my-list="@myList" />


And this is my typescript code to get the value above,



     let _myList = $('#myListHidden').data('my-list');  


And this is the return value,



      "System.Collections.Generic.List`1[MyProject.Data.ViewModels.MyViewModel]"


I just want to iterate through this collection. This is what I've tried



     for (let entry of _myList ) {
console.log(entry);
}


But it gives the output as System.Collections.Generic.List as string.

I want to iterate all the values inside this collection.



Edit



MyViewModel's properties are as follow,



    public long Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }









share|improve this question

























  • Not sure what you are doing but this will help --> forums.asp.net/t/1999070.aspx?Pass+2+Lists+to+a+View

    – Prashant Pimpale
    Jan 2 at 5:06











  • @PrashantPimpale, your reference link is just to iterate collection inside Razor code. what I want is inside Typescript

    – Steven Sann
    Jan 2 at 5:10











  • console.log(_myList) ?

    – Prashant Pimpale
    Jan 2 at 5:12






  • 1





    @StevenSann, you need to create the same class for MyViewModel in typescript also and then map you Generic Lit in c# with Array of class in typescript.

    – er-sho
    Jan 2 at 5:26











  • @er-shoaib, I've edit my question , you can take reference for my ViewModel. Can you give me sample code to create class for this , and how can I map inside Typescript ??

    – Steven Sann
    Jan 2 at 5:34
















0















This is my .cshtml code,



  @ { var myList = (List<MyViewModel>)ViewBag.MyCollection; }
<input id="myListHidden" type="hidden" data-my-list="@myList" />


And this is my typescript code to get the value above,



     let _myList = $('#myListHidden').data('my-list');  


And this is the return value,



      "System.Collections.Generic.List`1[MyProject.Data.ViewModels.MyViewModel]"


I just want to iterate through this collection. This is what I've tried



     for (let entry of _myList ) {
console.log(entry);
}


But it gives the output as System.Collections.Generic.List as string.

I want to iterate all the values inside this collection.



Edit



MyViewModel's properties are as follow,



    public long Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }









share|improve this question

























  • Not sure what you are doing but this will help --> forums.asp.net/t/1999070.aspx?Pass+2+Lists+to+a+View

    – Prashant Pimpale
    Jan 2 at 5:06











  • @PrashantPimpale, your reference link is just to iterate collection inside Razor code. what I want is inside Typescript

    – Steven Sann
    Jan 2 at 5:10











  • console.log(_myList) ?

    – Prashant Pimpale
    Jan 2 at 5:12






  • 1





    @StevenSann, you need to create the same class for MyViewModel in typescript also and then map you Generic Lit in c# with Array of class in typescript.

    – er-sho
    Jan 2 at 5:26











  • @er-shoaib, I've edit my question , you can take reference for my ViewModel. Can you give me sample code to create class for this , and how can I map inside Typescript ??

    – Steven Sann
    Jan 2 at 5:34














0












0








0








This is my .cshtml code,



  @ { var myList = (List<MyViewModel>)ViewBag.MyCollection; }
<input id="myListHidden" type="hidden" data-my-list="@myList" />


And this is my typescript code to get the value above,



     let _myList = $('#myListHidden').data('my-list');  


And this is the return value,



      "System.Collections.Generic.List`1[MyProject.Data.ViewModels.MyViewModel]"


I just want to iterate through this collection. This is what I've tried



     for (let entry of _myList ) {
console.log(entry);
}


But it gives the output as System.Collections.Generic.List as string.

I want to iterate all the values inside this collection.



Edit



MyViewModel's properties are as follow,



    public long Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }









share|improve this question
















This is my .cshtml code,



  @ { var myList = (List<MyViewModel>)ViewBag.MyCollection; }
<input id="myListHidden" type="hidden" data-my-list="@myList" />


And this is my typescript code to get the value above,



     let _myList = $('#myListHidden').data('my-list');  


And this is the return value,



      "System.Collections.Generic.List`1[MyProject.Data.ViewModels.MyViewModel]"


I just want to iterate through this collection. This is what I've tried



     for (let entry of _myList ) {
console.log(entry);
}


But it gives the output as System.Collections.Generic.List as string.

I want to iterate all the values inside this collection.



Edit



MyViewModel's properties are as follow,



    public long Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }






c# typescript razor generic-collections






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:44









Fisch

3,46512138




3,46512138










asked Jan 2 at 5:02









Steven SannSteven Sann

556




556













  • Not sure what you are doing but this will help --> forums.asp.net/t/1999070.aspx?Pass+2+Lists+to+a+View

    – Prashant Pimpale
    Jan 2 at 5:06











  • @PrashantPimpale, your reference link is just to iterate collection inside Razor code. what I want is inside Typescript

    – Steven Sann
    Jan 2 at 5:10











  • console.log(_myList) ?

    – Prashant Pimpale
    Jan 2 at 5:12






  • 1





    @StevenSann, you need to create the same class for MyViewModel in typescript also and then map you Generic Lit in c# with Array of class in typescript.

    – er-sho
    Jan 2 at 5:26











  • @er-shoaib, I've edit my question , you can take reference for my ViewModel. Can you give me sample code to create class for this , and how can I map inside Typescript ??

    – Steven Sann
    Jan 2 at 5:34



















  • Not sure what you are doing but this will help --> forums.asp.net/t/1999070.aspx?Pass+2+Lists+to+a+View

    – Prashant Pimpale
    Jan 2 at 5:06











  • @PrashantPimpale, your reference link is just to iterate collection inside Razor code. what I want is inside Typescript

    – Steven Sann
    Jan 2 at 5:10











  • console.log(_myList) ?

    – Prashant Pimpale
    Jan 2 at 5:12






  • 1





    @StevenSann, you need to create the same class for MyViewModel in typescript also and then map you Generic Lit in c# with Array of class in typescript.

    – er-sho
    Jan 2 at 5:26











  • @er-shoaib, I've edit my question , you can take reference for my ViewModel. Can you give me sample code to create class for this , and how can I map inside Typescript ??

    – Steven Sann
    Jan 2 at 5:34

















Not sure what you are doing but this will help --> forums.asp.net/t/1999070.aspx?Pass+2+Lists+to+a+View

– Prashant Pimpale
Jan 2 at 5:06





Not sure what you are doing but this will help --> forums.asp.net/t/1999070.aspx?Pass+2+Lists+to+a+View

– Prashant Pimpale
Jan 2 at 5:06













@PrashantPimpale, your reference link is just to iterate collection inside Razor code. what I want is inside Typescript

– Steven Sann
Jan 2 at 5:10





@PrashantPimpale, your reference link is just to iterate collection inside Razor code. what I want is inside Typescript

– Steven Sann
Jan 2 at 5:10













console.log(_myList) ?

– Prashant Pimpale
Jan 2 at 5:12





console.log(_myList) ?

– Prashant Pimpale
Jan 2 at 5:12




1




1





@StevenSann, you need to create the same class for MyViewModel in typescript also and then map you Generic Lit in c# with Array of class in typescript.

– er-sho
Jan 2 at 5:26





@StevenSann, you need to create the same class for MyViewModel in typescript also and then map you Generic Lit in c# with Array of class in typescript.

– er-sho
Jan 2 at 5:26













@er-shoaib, I've edit my question , you can take reference for my ViewModel. Can you give me sample code to create class for this , and how can I map inside Typescript ??

– Steven Sann
Jan 2 at 5:34





@er-shoaib, I've edit my question , you can take reference for my ViewModel. Can you give me sample code to create class for this , and how can I map inside Typescript ??

– Steven Sann
Jan 2 at 5:34












1 Answer
1






active

oldest

votes


















2














You will need to serialize your collection and then output that serialized value as "Raw" (or else the razor engine will escape your JSON, and you don't want that)



@using Newtonsoft.Json;

@{
var myList = JsonConvert.SerializeObject(ViewBag.MyColection);
}

<input id="myListHidden" type="hidden" data-my-list="@Html.Raw(myList)" />


In this example, I use the Newtonsoft serializer. You can use it by installing the NuGet package.



The above will generate something like this:



<input id="myListHidden" type="text" data-my-list="[{"Id":1,"Name":"Bob","Active":true}]" />


You can then use the value how you wish



EDIT:



Note that if you do not use @Html.Raw() the razor engine will output this:



<input id="myListHidden" type="text" data-my-list="[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Bob&quot;,&quot;Active&quot;:true}]" />





share|improve this answer


























  • got it , additionally , what's the different between with or without using @Html.Raw ??

    – Steven Sann
    Jan 2 at 9:24











  • i updated my answer with an example of what the output would look like without @Html.Raw

    – Fisch
    Jan 3 at 4:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001427%2fhow-to-parse-c-sharp-generic-collection-list-in-typescript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You will need to serialize your collection and then output that serialized value as "Raw" (or else the razor engine will escape your JSON, and you don't want that)



@using Newtonsoft.Json;

@{
var myList = JsonConvert.SerializeObject(ViewBag.MyColection);
}

<input id="myListHidden" type="hidden" data-my-list="@Html.Raw(myList)" />


In this example, I use the Newtonsoft serializer. You can use it by installing the NuGet package.



The above will generate something like this:



<input id="myListHidden" type="text" data-my-list="[{"Id":1,"Name":"Bob","Active":true}]" />


You can then use the value how you wish



EDIT:



Note that if you do not use @Html.Raw() the razor engine will output this:



<input id="myListHidden" type="text" data-my-list="[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Bob&quot;,&quot;Active&quot;:true}]" />





share|improve this answer


























  • got it , additionally , what's the different between with or without using @Html.Raw ??

    – Steven Sann
    Jan 2 at 9:24











  • i updated my answer with an example of what the output would look like without @Html.Raw

    – Fisch
    Jan 3 at 4:17
















2














You will need to serialize your collection and then output that serialized value as "Raw" (or else the razor engine will escape your JSON, and you don't want that)



@using Newtonsoft.Json;

@{
var myList = JsonConvert.SerializeObject(ViewBag.MyColection);
}

<input id="myListHidden" type="hidden" data-my-list="@Html.Raw(myList)" />


In this example, I use the Newtonsoft serializer. You can use it by installing the NuGet package.



The above will generate something like this:



<input id="myListHidden" type="text" data-my-list="[{"Id":1,"Name":"Bob","Active":true}]" />


You can then use the value how you wish



EDIT:



Note that if you do not use @Html.Raw() the razor engine will output this:



<input id="myListHidden" type="text" data-my-list="[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Bob&quot;,&quot;Active&quot;:true}]" />





share|improve this answer


























  • got it , additionally , what's the different between with or without using @Html.Raw ??

    – Steven Sann
    Jan 2 at 9:24











  • i updated my answer with an example of what the output would look like without @Html.Raw

    – Fisch
    Jan 3 at 4:17














2












2








2







You will need to serialize your collection and then output that serialized value as "Raw" (or else the razor engine will escape your JSON, and you don't want that)



@using Newtonsoft.Json;

@{
var myList = JsonConvert.SerializeObject(ViewBag.MyColection);
}

<input id="myListHidden" type="hidden" data-my-list="@Html.Raw(myList)" />


In this example, I use the Newtonsoft serializer. You can use it by installing the NuGet package.



The above will generate something like this:



<input id="myListHidden" type="text" data-my-list="[{"Id":1,"Name":"Bob","Active":true}]" />


You can then use the value how you wish



EDIT:



Note that if you do not use @Html.Raw() the razor engine will output this:



<input id="myListHidden" type="text" data-my-list="[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Bob&quot;,&quot;Active&quot;:true}]" />





share|improve this answer















You will need to serialize your collection and then output that serialized value as "Raw" (or else the razor engine will escape your JSON, and you don't want that)



@using Newtonsoft.Json;

@{
var myList = JsonConvert.SerializeObject(ViewBag.MyColection);
}

<input id="myListHidden" type="hidden" data-my-list="@Html.Raw(myList)" />


In this example, I use the Newtonsoft serializer. You can use it by installing the NuGet package.



The above will generate something like this:



<input id="myListHidden" type="text" data-my-list="[{"Id":1,"Name":"Bob","Active":true}]" />


You can then use the value how you wish



EDIT:



Note that if you do not use @Html.Raw() the razor engine will output this:



<input id="myListHidden" type="text" data-my-list="[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Bob&quot;,&quot;Active&quot;:true}]" />






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 4:17

























answered Jan 2 at 6:42









FischFisch

3,46512138




3,46512138













  • got it , additionally , what's the different between with or without using @Html.Raw ??

    – Steven Sann
    Jan 2 at 9:24











  • i updated my answer with an example of what the output would look like without @Html.Raw

    – Fisch
    Jan 3 at 4:17



















  • got it , additionally , what's the different between with or without using @Html.Raw ??

    – Steven Sann
    Jan 2 at 9:24











  • i updated my answer with an example of what the output would look like without @Html.Raw

    – Fisch
    Jan 3 at 4:17

















got it , additionally , what's the different between with or without using @Html.Raw ??

– Steven Sann
Jan 2 at 9:24





got it , additionally , what's the different between with or without using @Html.Raw ??

– Steven Sann
Jan 2 at 9:24













i updated my answer with an example of what the output would look like without @Html.Raw

– Fisch
Jan 3 at 4:17





i updated my answer with an example of what the output would look like without @Html.Raw

– Fisch
Jan 3 at 4:17




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001427%2fhow-to-parse-c-sharp-generic-collection-list-in-typescript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas