示出System.Web.Helpers.Chart在从模型的局部视图 [英] Showing System.Web.Helpers.Chart in a partial view from the model

查看:148
本文介绍了示出System.Web.Helpers.Chart在从模型的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在System.Web.Helpers命名空间尝试图表帮手。

//:根据 HTTP

www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

我做了.cshtml查看图表,但我想保持它在视图模型来代替。

除了当我试图使其在该网站更小的图像没有问题。

我认为最干净的解决方案是创建一个共享的局部视图渲染模式,从图

_graph.cshtml

  @model System.Web.Helpers.Chart@ Model.Write()

然后在适当的网站以某种方式使这个局部视图。我试过几个版本,但似乎无法得到它的工作。

Website.cshtml

 < D​​IV>
    &所述; H2>一种曲线图所述上方某一首标; / H2>
    &所述; IMG SRC =@ Html.Partial(_图,Model.TheChart)/>
< / DIV>

这不工作,我不能确定如何做到这一点。只有想不到我能想到用图表继承暴露图表接口,并让这成为_graph.cshtml模型目前正在所有车型。

 < IMG SRC =_ graph.cshtml/>

但是,如果使用这种模型不能确定。

任何意见?


解决方案

 < D​​IV>
    &所述; H2>一种曲线图所述上方某一首标; / H2>
    &所述; IMG SRC =@ Url.Action(DrawChart)/>
< / DIV>

,然后你可以有一个控制器动作:

 公众的ActionResult DrawChart()
{
    MyViewModel模型= ...
    返回查看(模型);
}

和将绘制图表相应的视图( DrawChart.cshtml

  @model MyViewModel@ {
    // TODO:使用数据从模型绘制图表    VAR myChart =新图(宽度:600,高度:400)
        .AddTitle(图表标题)
        .AddSeries(
            名称:雇员,
            xValue:新[] {彼得,安德鲁,朱莉,玛丽,戴夫},
            yValues​​:新[] {2,6,4,5,3})
        。写();
}

和渲染的结果是:


So I was trying out the Chart helpers in the System.Web.Helpers namespace.

according to http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

I make the chart in a .cshtml view but I wanted to keep it in the ViewModel instead.

No problem except for when I'm trying to render it as a smaller image in the website.

I thought the cleanest solution would be to create one shared partial view to render graphs from models

_graph.cshtml

@model System.Web.Helpers.Chart

@Model.Write()

And then render this partial view somehow in the proper websites. I tried a few versions but can't seem to get it to work.

Website.cshtml

<div>
    <h2>Some header above a graph</h2>
    <img src="@Html.Partial("_graph", Model.TheChart)" />
</div>

This doesn't work and I'm not certain how to do this. Only think I can think of now is making all models with charts inherit an Interface that exposes Chart and let that be the model for _graph.cshtml.

<img src="_graph.cshtml" />

But not sure if the this uses the Model.

Any opinions?

解决方案

<div>
    <h2>Some header above a graph</h2>
    <img src="@Url.Action("DrawChart")" />
</div>

and then you could have a controller action:

public ActionResult DrawChart()
{
    MyViewModel model = ...
    return View(model);
}

and a corresponding view that will draw the chart (DrawChart.cshtml):

@model MyViewModel

@{
    // TODO: use the data from the model to draw a chart

    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();
}

and the rendered result:


这篇关于示出System.Web.Helpers.Chart在从模型的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆