C# 使用MVC、.net c显示图表#
我试图在我的mvc应用程序上显示一些图表,但出现了一些错误。我正在开发localhost。我有一个名为ReportChart的cshtml文件C# 使用MVC、.net c显示图表#,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我试图在我的mvc应用程序上显示一些图表,但出现了一些错误。我正在开发localhost。我有一个名为ReportChart的cshtml文件 @{ var myChart = new Chart(width: 600, height: 400) .AddTitle("Chart Title") .AddSeries( name: "Employee", xValue: new[] { "Peter", "Andrew", "Julie", "Ma
@{
var myChart = new Chart(width: 600, height: 400)
.AddTitle("Chart Title")
.AddSeries(
name: "Employee",
xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
yValues: new[] { "2", "6", "4", "5", "3" })
.Write();
}
以及使用该图表的另一个文件:
<body>
<h1>Chart Example</h1>
<p>The following chart is generated by the <em>ReportChart.cshtml</em> file:</p>
<p><img src="ReportChart.cshtml" alt="Cricketers" /> </p>
图表示例
以下图表由ReportChart.cshtml文件生成:
唯一的问题是该网页不显示任何图像:/不,它在MVC中不起作用。您应该在控制器操作方法中创建图表:
//I CUT THE CODE WHERE I construct string[] t1 and int[] t2 these are just arrays
public ActionResult EfficiencyChart(string pid) {
var myChart = new Chart(width: 1000, height: 600)
.AddTitle("Employee's Efficiency")
.AddSeries(
name: "Employee",
xValue: t2,
yValues: t1)
.Write();
myChart.Save("~/Content/chart" + user.Id, "jpeg");
// Return the contents of the Stream to the client
return base.File("~/Content/chart" + user.Id, "jpeg");
}
然后在Razor视图中:
<img src="@Url.Action("EfficiencyChart", "NAME_OF_THE_CONTROLLER", new { pid = @Model.Id })" />
不,它在MVC中不起作用。您应该在控制器操作方法中创建图表:
//I CUT THE CODE WHERE I construct string[] t1 and int[] t2 these are just arrays
public ActionResult EfficiencyChart(string pid) {
var myChart = new Chart(width: 1000, height: 600)
.AddTitle("Employee's Efficiency")
.AddSeries(
name: "Employee",
xValue: t2,
yValues: t1)
.Write();
myChart.Save("~/Content/chart" + user.Id, "jpeg");
// Return the contents of the Stream to the client
return base.File("~/Content/chart" + user.Id, "jpeg");
}
然后在Razor视图中:
<img src="@Url.Action("EfficiencyChart", "NAME_OF_THE_CONTROLLER", new { pid = @Model.Id })" />
试试看
<p><img src="@Url.Action("ReportChart")" alt="Cricketers" /> </p>
public ActionResult ReportChart()
{
return PartialView();
}
公共行动结果报告图表()
{
返回PartialView();
}
试试看
<p><img src="@Url.Action("ReportChart")" alt="Cricketers" /> </p>
public ActionResult ReportChart()
{
return PartialView();
}
公共行动结果报告图表()
{
返回PartialView();
}
我们应该知道什么是ReportChart吗?@RobertHarvey我使用这个网站来了解我的代码,如果你直接在浏览器中打开图表,它会显示吗。ReportChart.cshtml实际上不是图像。您正在尝试将文件渲染为非图像的图像…尽管这正是您链接的教程所要做的。抱歉,但我被难住了。我们应该知道什么是ReportChart吗?@RobertHarvey我使用这个网站来了解我的代码,如果你直接在浏览器中打开图表,它会显示吗?啊,现在我知道了。ReportChart.cshtml实际上不是图像。您正在尝试将文件渲染为非图像的图像…尽管这正是您链接的教程所要做的。对不起,我被难住了。再问一个问题,如果我想查询数据库,我应该在我的控制器中进行吗?@user2002274是的。如果您希望直接从db(通过DbContext)获取数据,则应在controller中完成。这可以正常工作,但唯一的问题是此操作会打开一个映像。。我想有我的布局,在我的布局中有两个图表和HiperLink,以及其他东西。。我该怎么做?再问一个问题,如果我想查询数据库,我应该在我的控制器中进行查询?@user2002274是的。如果您希望直接从db(通过DbContext)获取数据,则应在controller中完成。这可以正常工作,但唯一的问题是此操作会打开一个映像。。我想有我的布局,在我的布局中有两个图表和HiperLink,以及其他东西。。我该怎么做?