我正在使用MVC 5网络应用程序中的外部Javascript文件将图表呈现给视图。该图表使用的模型只是可访问的服务器端,因此我使用内联js在JSON中编码模型,如下所示,然后调用传入JSON字符串的外部函数。
<script type="text/javascript">
$(document).ready(function () {
var JSONdata = @Html.Raw(Json.Encode(Model.PastModels));
var count = @Html.Raw(Json.Encode(Model.PastModels.Count));
createChart(JSONdata, count);
});
该功能在外部文件中定义。
function createChart(JSONdata, count) {
var dates = [];
var scores = [];
var name = ""
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].TotalScoreT;
}
var chart = 'totalChart';
name = " Total Score";
renderChart(chart, name);
$('.nav-tabs a').click(function (e) {
e.preventDefault();
var tab = this.id;
switch (tab) {
case 'socialTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].SocialPhobiaT;
}
chart = socialChart;
name = ' Social Phobia';
break;
case 'panicTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].PanicDisorderT;
}
chart = panicChart;
name = ' Panic Disorder';
break;
case 'depressionTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].MajorDepressionT;
}
chart = depressionChart;
name = ' Major Depression';
break;
case 'separationTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].SeparationAniextyT;
}
chart = separationChart;
name = ' Separation Anxiety';
break;
case 'obsessiveTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].ObsessiveCompulsiveT;
}
chart = obsessiveChart;
name = ' Obsessive Compulsive Disorder';
break;
case 'generalTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].GeneralizedAnxietyT;
}
chart = generalChart;
name = ' Generalized Anxiety';
break;
case 'totalTab':
for (i = 0; i < count; i++) {
dates[i] = JSONdata[i].TheDate;
scores[i] = JSONdata[i].TotalScoreT;
}
chart = totalChart;
name = ' Total Score';
break;
}
renderChart(chart, name);
});
}
我使用部分包含外部文件。
@section HeadValues{
<script src="https://code.highcharts.com/highcharts.js" type="text/javascript"></script>
<script src="https://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
<script src="~/scripts/RCADSResultPage.js" type="text/javascript"></script>
}
然而,无论何时加载页面,我都会收到错误'function createChart is undefined'。
我们非常感谢任何建议。
感谢。
答案 0 :(得分:0)
由于外部脚本未成功加载或在$(document).ready函数调用之前调用,因此出现上述问题。 确保在页面上的createChart函数调用之前加载脚本部分。
@section HeadValues{
<script src="https://code.highcharts.com/highcharts.js" type="text/javascript"></script>
<script src="https://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
<script src="~/scripts/RCADSResultPage.js" type="text/javascript"></script>
}
希望它会帮助你。
快乐编码
由于