ASP.NET MVC 3 Charting和HTTPPOST

时间:2014-11-12 20:11:35

标签: asp.net asp.net-mvc-3

在Asp.Net MVC 3中工作。

控制器代码:

    [HttpPost]
    public ActionResult MyChart(string sDate)
    {
        /*Call Entity Database*/
        Contact_Center_DashboardEntities3 db = new Contact_Center_DashboardEntities3();
        var results01 = db.sp_Report_Hybrid_Metrics("9 September", sDate, "Auto", "R2/R8", "Claims", "Claims", "R02_R08.AUTO.CLM.FMN.Q").ToList();


        var chart = new Chart();
        chart.Width = 1000;
        chart.Height = 200;

        var area = new ChartArea();
        // configure your chart area (dimensions, etc) here.
        chart.ChartAreas.Add(area);

        area.AxisX.MajorGrid.Enabled = false;

        area.AxisY.MajorGrid.Enabled = false;

        area.AxisY2.MajorGrid.Enabled = false;
        area.AxisY2.LabelStyle.Format = "0%";

        // create and customize your data series.
        var series = new Series();
        foreach (var item in results01)
        {
            series.Points.AddXY(item.Date, item.Volume);
        }
        series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
        series.ChartType = SeriesChartType.Column;
        series.YAxisType = AxisType.Primary;


        // create and customize your data series.
        var seriesSecondary = new Series();
        foreach (var item in results01)
        {
            seriesSecondary.Points.AddXY(item.Date, item.XferPer);
        }
        seriesSecondary.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
        seriesSecondary.ChartType = SeriesChartType.Line;
        seriesSecondary.YAxisType = AxisType.Secondary;
        seriesSecondary.LabelFormat = "0%";
        seriesSecondary.IsValueShownAsLabel = true;


        chart.Series.Add(series);
        chart.Series.Add(seriesSecondary);

        var returnStream = new MemoryStream();
        chart.ImageType = ChartImageType.Png;
        chart.SaveImage(returnStream);
        returnStream.Position = 0;
        return new FileStreamResult(returnStream, "image/png");

    }

表格查看代码:

@using (Html.BeginForm("MyChart", "Home", FormMethod.Post))
{
    <fieldset>
        <div class="editor-field">
            <input type="submit" value="09/06/2014" name = "sDate"/>
        </div>
    </fieldset>

}

当用户点击“提交”按钮时,该值将传递给&#34; MyChart&#34; ActionResult正确,但然后带我到MyChart视图。我需要它做的是传递值,保持在表视图上,并刷新图表。

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

@using (Ajax.BeginForm("MyChart", "Home", new AjaxOptions{HttpMethod= "Post", UpdateTargetId = "chartContainer", InsertionMode = InsertionMode.Replace}))
{
    <fieldset>
        <div class="editor-field">
            <input type="submit" value="09/06/2014" name = "sDate"/>
        </div>
    </fieldset>
}

<div id="chartContainer">
</div>

请求由ajax完成,并将请求的响应插入到由updatetargetid寻址的元素中。如果单击一次后应删除提交按钮,请将表单放在div中。否则,按下提交按钮,图表将会超时刷新。