0

I am developing an application in .NET Core MVC. In my View page, I have a table like this table here. Which generates using foreach loop.

foreach (var items in Model){
   // generate Table
}

enter image description here

What I want is, Not to show this table. Instead, When the Page loads, Pass this values to JavaScript Array and show some dynamic charts.

For example, The Amount column in the table. I want to get the amount values in javascript like:

$( document ).ready(function() {
  var amountValues = [1000, 3000, 2000, 1100];
});
2
  • Check stackoverflow.com/questions/12752834/… if it helps you Commented Mar 24, 2019 at 13:49
  • Is there any reason you need to change the amountValues? Why don't set it at the server side by Model? What do you mean by dynamic charts? What is your expected result? Commented Mar 25, 2019 at 2:13

1 Answer 1

1

You could serialize the Model into a javascript class and use as you want:

var myModel = @Html.Raw(Json.Serialize(Model));
for (var item in myModel)
{ 
   var amountValue = item.Amount;
   //push to array etc.
}
Sign up to request clarification or add additional context in comments.

2 Comments

why the surrounding @{ }? That's a code block so nothing should be output in the html.
yea you are right, must have been some rapid prototyping a little too rapid :) It's removed now

Your Answer

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