Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:01:20+00:00 2026-05-28T18:01:20+00:00

I am trying to build out a google chart in an mvc app. Here

  • 0

I am trying to build out a google chart in an mvc app.

Here is a snippet of google Chart javascript

function drawChart() {
 var data = new google.visualization.DataTable();
    data.addColumn('string', 'Task');
    data.addColumn('number', 'Hours per Day');
    data.addRows([

      ['Work',    11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

}

what I would like to do is essentially replace the data.addRows line above with a for loop iterating through the items in my model. I can iterate just fine in the view outside of the tag like so of course:

 "@foreach (var item in Model) {
    <div>@Html.DisplayFor(modelItem => item.Customer.Name)</div>
  }"

I cannot seem to find a solution to iterate through my model INSIDE a tag. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:01:21+00:00Added an answer on May 28, 2026 at 6:01 pm

    Assuming you have a view model:

    public class MyViewModel
    {
        public object[][] Values { get; set; }
    }
    

    in which you store some values and pass along to the view:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Values = new[]
                {
                    new object[] { "Work",     11 },
                    new object[] { "Eat",      2 },
                    new object[] { "Commute",  2 },
                    new object[] { "Watch TV", 2 },
                    new object[] { "Sleep",    7 },
                }
            };
            return View(model);
        }
    }
    

    in your view you could JSON encode it:

    @model MyViewModel
    <script type="text/javascript">
        function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Task');
            data.addColumn('number', 'Hours per Day');
            data.addRows(@Html.Raw(Json.Encode(Model.Values)));
        }
    </script>
    

    which will be rendered in the final markup as:

    <script type="text/javascript">
        function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Task');
            data.addColumn('number', 'Hours per Day');
            data.addRows([["Work",11],["Eat",2],["Commute",2],["Watch TV",2],["Sleep",7]]);
        }
    </script>
    

    And you shouldn’t be worried at all about having values that contain single or double quotes which could potentially break your javascript because you have used a JSON serializer instead of manually building it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build out a Google News Sitemap in addition to a normal
I'm trying to build a Google Chart to show some uptime and downtime percentages,
I've implemented push notifications in my Android app and am trying to build out
This is really getting me frustrated. I'm trying to build a custom JavaScript app
I am trying to build chat application with long polling mechanism on Google app
I am trying to build a simple app using google app engine, with java
I am trying to build an example app in Google App Engine using django-nonrel.
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I'm trying to build out a mysql database design for a project. The problem
I am trying build a jQuery EasyUI datagrid or treegrid out of a large

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.