My challenge involves plotting a chart using values from an object's list in my view with High Chart. However, I face the issue of accessing my model from JavaScript.
public class MortgageCalculator
{
public List<double> interest_month = new List<double>();
public void calculations()
{
for(int i=1;i<=this.nPer;i++)
{
this.interest_month.Add(Math.Round(-Financial.IPmt((this.interest / 12) / 100, Convert.ToDouble(i), this.nPer, this.loan_amount),2));
}
}
}
In the Controller Code: I receive user input for calculations, update my model, and then redirect to the Results page where the chart of interest values should be displayed.
[HttpGet]
public ActionResult M_Calculator()
{
var mortgageCalculator = new MortgageCalculator();
return View(mortgageCalculator);
}
[HttpPost]
public ActionResult M_Calculator(MortgageCalculator mortgageCalculator)
{
UpdateModel(mortgageCalculator);
//call calculation functions here
mortgageCalculator.Amortization();
return View("Results",mortgageCalculator);
}
In the View Code:
@model Calculators.Models.MortgageCalculator
@{
ViewBag.Title = "Results";
}
@Scripts.Render("~/Scripts/lineChart.js")\
<div id="container" style="min-width:310px; height:400px; margin:0 auto">
@section scripts
{
<script src="~/Scripts/highcharts.js"></script>
<script src="~/Scripts/highcharts.src.js"></script>
<script src="~/Scripts/lineChart.js"></script>
}
</div>
I am seeking guidance on how to access the list from the model in my lineChart.js JavaScript file to plot the chart. It seems like I may need to use JSON, but I'm uncertain about how to proceed. Any assistance would be greatly appreciated.
Thank you