Search code examples
c#chartsmorris.jsblazor.net-core-3.0

Accessing array by <path d=""> using blazor


I am trying to create a chart using morris.js. I have my data in an array. I am trying to loop through my array inside of my attribute. I am not able to access the array. (Blazor problem)?

I haven't be able to get the chart to work. I put in sample data an was able to get the chart to generate but am not able to with my array. When i try @integerArray and then type "." nothing pops up so i'm assuming i'm not doing it right.

<div class="panel-body">
    <h4 class="text-center">H4 Tag</h4>
    <div id="morris-line-chart" class="height-sm">
        <svg height="300" version="1.1">
            <path fill="none" stroke="#348fe2" d="attr="@foreach(var item in @integerArray){}""></path>
        </svg>
    </div>
</div>


@code{

    public class DataAccess
    {

        ApplicationDbContext _Context;

        public DataAccess(ApplicationDbContext applicationDbContext)
        {
            _Context = applicationDbContext;
        }

        void OnGet()
        {
            var data = (from ovenModel in _Context.OvenDataKey
                        select ovenModel).ToArray();
            int[] integerArray = new int[data.Count()];

            for (int i = 0; i <= integerArray.Length; i++)
            {
                integerArray[i] = data[i].Temp;
            }
        }
    }
}

I need to print out a chart on the HTML using the array i've created in the @code. I just don't know how to access the data inside the array.


Solution

  • Define the variable integerArray outside your class, in the @code block, like that:

    @code {
       int[] integerArray {get;set;}
    }
    

    The problem is that Blazor cannot access a local variable in your class. However, you may define it in your class as static

    • I guess you're using server-side Blazor, right ? Because if you're not, you cannot use Entity Framework on the client ( client-side Blazor)

    • You should know that Blazor is entirely different than Razor Pages, though both employ Razor syntax. You should learn the Blazor Component Model and how components communicate with each other. If I'm wrong about this assumption, I apologize...

    Hope this helps...