Я пытаюсь создать веб-сайт с использованием ASP.NET (C #) и MVC. Я всюду искал ответ и надеюсь здесь помочь.
Идеи и код взяты из: http://www.codecapers.com/post/Build-a-Dashboard-With-Microsoft-Chart-Controls.aspx
Мне удалось заставить вышеуказанное работать с простым проектом MVC и поддельными данными.
Я пытаюсь отобразить диаграмму в частичном представлении и получаю следующую ошибку:
Ссылка на объект не установлена для экземпляра объекта
Я не уверен, почему я получаю это. При отладке список показывает, что в нем есть один график. Я также могу распечатать информацию о диаграмме (то есть высоту, ширину), которые были установлены в модели. Я просто не могу сделать это в частичном представлении. Создание диаграммы в виде изображения мне не пригодится.
Заранее спасибо за помощь!
Я не думаю, что он имеет какое-либо отношение к web.config, но в любом случае включит его:
web.config
<configuration>
<appSettings>
<add key="ChartImageHandler" value="Storage=file;Timeout=20;Url=/tempImages/;"/>
</appSettings>
<system.web>
<httpHandlers>
<add verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
<add verb="GET,HEAD,POST" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</httpHandlers>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880"/>
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
</namespaces>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<remove name="ChartImageHandler"/>
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Контроллер
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dashboard.WebUI.Models;
namespace Dashboard.WebUI.Controllers
{
public class DashboardController : Controller
{
//
// GET: /Dashboard/
public ActionResult Index()
{
//LineSummaryModel model = new LineSummaryModel();
PieChartModel model = new PieChartModel();
return View(model);
}
}
}
Модель
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.DataVisualization;
using System.Web.UI.DataVisualization.Charting;
namespace Dashboard.WebUI.Models {
public class PieChartModel {
public PieChartModel() {
}
internal class PieChartData {
public String Title { get; set; }
public String[] Data { get; set; }
public double[] xValues { get; set; }
public double[] yValues { get; set; }
}
private Chart BuildServerPieChart() {
String[] temp = { "France", "Canada", "Germany", "USA", "Italy" };
double[] x = {1.0, 2.3, 4.5, 5.5, 5.6 };
double[] y = {1.0, 2.0, 3.0, 4.0, 5.0 };
PieChartData data = new PieChartData {
Title = "Test Server",
Data = temp,
xValues = x,
yValues = y
};
return BindChartData(data);
}
private Chart BindChartData(PieChartData data) {
Chart chart = new Chart();
chart.Width = 150;
chart.Height = 300;
chart.Attributes.Add("align", "left");
chart.Titles.Add(data.Title);
chart.ChartAreas.Add(new ChartArea());
chart.Series.Add(new Series());
chart.Legends.Add(new Legend("Countries"));
chart.Legends[0].TableStyle = LegendTableStyle.Auto;
chart.Legends[0].Docking = Docking.Bottom;
chart.Series[0].ChartType = SeriesChartType.Pie;
for(int i = 0; i < data.xValues.Length; i++){
double x = data.xValues[i];
double y = data.yValues[i];
int ptIdx = chart.Series[0].Points.AddXY(x, y);
var c = data.Data[i];
DataPoint pt = chart.Series[0].Points[ptIdx];
pt.ToolTip = c + ""+ x;
}
chart.Series[0].Legend = "Countries";
return chart;
}
public List<Chart> PieCharts {
get {
List<Chart> charts = new List<Chart>();
charts.Add(BuildServerPieChart());
return charts;
}
}
}
}
Мастер-страница (частичная)
<!-- CONTENT -->
<div id="fps-main" style="width: 100%">
<div>
<asp:ContentPlaceHolder ID="LineSummary" runat="server">
</asp:ContentPlaceHolder>
</div>
<div>
<asp:ContentPlaceHolder ID="MiddleRow" runat="server">
</asp:ContentPlaceHolder>
</div>
<div>
<asp:ContentPlaceHolder ID="BottomRow" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
<!-- //CONTENT -->
index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Dashboard.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<asp:Content ContentPlaceHolderID="TopSpotLight" runat="server">
<% Html.RenderPartial("TopSpotlight"); %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LineSummary" runat="server">
<% Html.RenderPartial("LineSummary"); %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BottomRow" runat="server">
<h2>Section 3</h2>
</asp:Content>
Parital View (строка Summary.ascx)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Dashboard.WebUI.Models.PieChartModel>" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<h2>Line Summary</h2>
<%
//Error Occurs here but I don't think this is the problem
foreach (Chart chart in Model.PieCharts) {
try {
//In this section, I tried to render the control a couplie
//different ways. Neither worked.
PieSummaryChartPanel.Controls.Add(chart);
//This prints out the correct height I set in model
Response.Write(chart.Height + "<br />");
HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output);
chart.RenderControl(writer);
} catch (System.NullReferenceException e) {
Response.Write("ERROR: " + e.Message);
}
}
//Response.Write("Test " + LineSummaryChartPanel.BorderWidth.Value);
//}
%>
<asp:Panel ID="PieSummaryChartPanel" runat="server"></asp:Panel>
Я не уверен, почему, если оно пустое, я могу распечатать свойства объекта. Спасибо за вашу помощь.
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 11:
Line 12:
Line 13: foreach (Chart chart in Model.PieCharts) {
Line 14: //if (chart != null && PieSummaryChartPanel != null) {
Line 15: // try {
Source File: c:\Users\Scott\Documents\Projects\Visual Studio 2010\Projects\DashboardSolution\Dashboard.WebUI\Views\Shared\LineSummary.ascx Line: 13
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.DataVisualization.Charting.ChartHttpHandler.GetHandlerUrl() +96
System.Web.UI.DataVisualization.Charting.ChartHttpHandler.GetUrl(String query, String fileKey, String currentGuid) +32
System.Web.UI.DataVisualization.Charting.ChartHttpHandler.GetChartImageUrl(MemoryStream stream, String imageExt) +339
System.Web.UI.DataVisualization.Charting.Chart.Render(HtmlTextWriter writer) +420
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +10
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.views_shared_linesummary_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Scott\Documents\Projects\Visual Studio 2010\Projects\DashboardSolution\Dashboard.WebUI\Views\Shared\LineSummary.ascx:13
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060