I am completely new to ASP.NET MVC and I am trying to show data from a database in a Highchart using Visual Studio 2015. In my controller, I have the following code to retrieve data from the database:
namespace HelloWorld.Controllers
{
public class SecondlyReadingDatasController : ApiController
{
private cloudsqlEntities db = new cloudsqlEntities();
// GET: api/SecondlyReadingDatas
public IQueryable<SecondlyReading> GetSecondlyReadings()
{
SecondlyReading sec = db.SecondlyReadings.First();
return db.SecondlyReadings;
}
}
}
This is my model:
namespace HelloWorld.Models
{
using System;
using System.Collections.Generic;
public partial class SecondlyReading
{
public int Id { get; set; }
public int ChannelID { get; set; }
public string TimeStamp { get; set; }
public double RMSVoltage { get; set; }
public double Frequency { get; set; }
public double RMSCurrent { get; set; }
public double RealPower { get; set; }
public double ReactivePower { get; set; }
public double ApparentPower { get; set; }
public double PowerFactor { get; set; }
public string DeviceId { get; set; }
}
}
When I enter /api/SecondlyReadingDatas in my browser, I can retrieve data in JSON format. However, my goal is to display this data in a line chart using Highcharts. I am aware that I need to include code like the following to visualize the data:
<script type="text/javascript">
$.ajax({
url: 'http://localhost/TestWebsite/api/SecondlyReadingDatas',
success: function(singleSeries) {
Highcharts.chart('container', {
series: [singleSeries]
});
}
});
</script>
I have also added DotNet.HighChart to my project:
https://i.sstatic.net/NKt4m.jpg
I have two main queries:
- Is there any distinction between downloading the Highchart library from the website and referencing it in Visual Studio, and installing DotNet.HighChart in Visual Studio itself?
- Where should I insert the
code? In the model, controller, or _Layout.cshtml?<script type="text/javascript">