我有一个控制器,它在调用时会创建一个文件内容结果,但是当我尝试将其设置为图像的源时,图像不会显示。我已经使用 firebug 进行调试,并且已经看到某些内容返回到视图中,但看起来它认为它无效。
控制器
[HttpPost]
public ActionResult BespokeAnalysis(MemberModel model)
{
if (ModelState.IsValid)
{
var CostVariations = CostVariationRepository.GetAll();
if (model.SelectedYears != null && model.SelectedTypes != null && model.SelectedCostTypes != null)
{
var variations = CostVariations.Where(m => model.SelectedYears.Contains(m.Year));
variationresults = variations.Where(m => model.SelectedTypes.Contains(m.CategoryId)).ToList();
selectedtypes = model.SelectedCostTypes.ToList();
}
}
return Json(new { chartUrl = Url.Action("CreateChart", "Member", new { chartType = SeriesChartType.Column, selectedtypes = selectedtypes, variationresults = variationresults }) });
}
剧本:
$(function () {
$('form').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#result').html(
$('<img/>', {
src: result.chartUrl,
alt: 'this is a super chart'
})
);
}
});
return false;
});
});
风景:
<div id="slideleft" class="tslide">
<div id="result"></div>
<img id="image" src="" alt="none"/>
@using (Html.BeginForm())
{
<div class="inner"><a href="#" class="toggle">Parameters</a>
<div class="dropdowntoggle">
<div class="buttonnarrow">Select</div>
<div class="ui-widget-content">
@Html.CheckBoxListFor(model => model.SelectedSectors, new MultiSelectList(Model.Sectors, "Id", "Name", Model.SelectedSectors))
</div>
</div>
</div>
<input type="submit" value="Create" onfocus="this.blur()" class="createbutton"/>
</div>
}
</div>
</div>
返回 ActionResult 的方法
public ActionResult CreateChart(SeriesChartType chartType)
{
Chart chart = new Chart();
chart.Width = 700;
chart.Height = 400;
chart.BackColor = Color.FromArgb(211, 223, 240);
chart.BorderlineDashStyle = ChartDashStyle.Solid;
chart.BackSecondaryColor = Color.White;
chart.BackGradientStyle = GradientStyle.TopBottom;
chart.BorderlineWidth = 1;
chart.Palette = ChartColorPalette.BrightPastel;
chart.BorderlineColor = Color.FromArgb(26, 59, 105);
chart.RenderType = RenderType.BinaryStreaming;
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.AntiAliasing = AntiAliasingStyles.All;
chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;
foreach (var type in selectedtypes)
{
switch (type)
{
case 1:
foreach (var result in variationresults)
{
Dictionary<string, double> retVal = new Dictionary<string, double>();
retVal.Add("Stuff1", Math.Round(result.Stuff1, 3, MidpointRounding.ToEven));
chart.Series.Add(CreateSeries(retVal, result.Description + type, Color.Lavender, chartType));
}
break;
case 2:
foreach (var result in variationresults)
{
Dictionary<string, double> retVal = new Dictionary<string, double>();
retVal.Add("Stuff2", Math.Round(result.Stuff2, 3, MidpointRounding.ToEven));
chart.Series.Add(CreateSeries(retVal, result.Description + type, Color.Aqua, chartType));
}
break;
case 3:
foreach (var result in variationresults)
{
Dictionary<string, double> retVal = new Dictionary<string, double>();
retVal.Add("Stuff3", Math.Round(result.Stuff3, 3, MidpointRounding.ToEven));
chart.Series.Add(CreateSeries(retVal, result.Description + type, Color.Azure, chartType));
}
break;
case 4:
foreach (var result in variationresults)
{
Dictionary<string, double> retVal = new Dictionary<string, double>();
retVal.Add("Stuff4", Math.Round(result.Stuff4, 3, MidpointRounding.ToEven));
chart.Series.Add(CreateSeries(retVal, result.Description + type, Color.Bisque, chartType));
}
break;
default:
foreach (var result in variationresults)
{
Dictionary<string, double> retVal = new Dictionary<string, double>();
retVal.Add("Crew Other", Math.Round(result.Crew_Other, 3, MidpointRounding.ToEven));
chart.Series.Add(CreateSeries(retVal, result.Description + type, Color.Lavender, chartType));
}
break;
}
}
chart.ChartAreas.Add(CreateChartArea());
using (var ms = new MemoryStream())
{
chart.SaveImage(ms, ChartImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
return File(ms.ToArray(), "image/png", "Variations.png");
}
}
任何帮助将不胜感激。