我正在尝试使用剃须刀显示列表中每个元素的图形(尝试使用 url.action 但没有任何效果......现在,当我在元素上运行并编写图表时,页面仅显示在图片上,没有其他内容(甚至不是页面的正文(。有人知道我能做什么吗?我的代码:
if (analyzeList != null)//list is not null
{
foreach (var document in analyzeList)//for each record
{
i = 0;
@:<B>documentID : @document._id </B><br />
foreach (var posture in document.PosturesList)//for each posture, write it
{
@:posture - @posture -> @document.DurationsList[i] seconds <br />
i++;
}//endforeach posture
<div>
@{
new Chart(width: 800, height: 200)//create the chart
.AddSeries(chartType: "column",
xValue: document.PosturesList.AsEnumerable(),//set x values
yValues: document.DurationsList.AsEnumerable()).Write();//set y values
}
</div>
}//end for each document
}//end if list is not null
}
您只看到一个图表图像,因为Chart.Write
方法写入输出流,并且不会执行任何其他内容。
您应该使用图像元素并将其源设置为一个操作方法的 url,该方法使用 Chart API 呈现图形。因此,创建一个操作方法,该方法返回具有用于呈现图表的代码的部分视图
public ActionResult DrawChart()
{
return PartialView();
}
在DrawChart.cshtml
视图中,您将拥有呈现图表图像的代码。
现在,在主视图中,您将调用此操作方法并将其设置为图像src
<img src="@Url.Action("DrawChart", "YourControllerName")" />
可以将唯一 id 作为查询字符串参数传递给 DrawChart
方法,以获取每个图表所需的数据。
public ActionResult DrawChart(int documentId)
{
// to do : Get data needed to render chart using documentId and pass to the view
return PartialView();
}
现在在主视图中,请确保在使用Url.Action
助手时传递此 documentId 路由值项
foreach (var document in analyzeList)//for each record
{
<img src="@Url.Action("DrawChart", "YourControllerName",
new { documentId=document._id )" />
}