【问题标题】:Control from controller whether a javascript code at my asp view will be executed or not从控制器控制是否执行我的 asp 视图中的 javascript 代码
【发布时间】:2014-02-01 04:32:23
【问题描述】:

我正在用 ASP.NET MVC4 开发一个系统。我有一个用 javascript 编写的代码,它在页面启动时执行,以显示一些学生表现的图表。

图形的代码如下

<script>
    window.onload = function () {
        var r = Raphael("holder"),
            txtattr = { font: "20px sans-serif" };

        var lines = r.linechart(30, 30, 600, 440,<%= Json.Encode(ViewBag.dates) %>,<%= Json.Encode(ViewBag.Grades)%>, {axisxstep : 6,nostroke: false, axis: "0 0 1 1", symbol: "circle", smooth: false }).hoverColumn(function () {
            this.tags = r.set();
            for (var i = 0, ii = this.y.length; i < ii; i++) {
                this.tags.push(r.tag(this.x, this.y[i], Number(this.values[i]).toFixed(5), 160, 10).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
            }
        });
</script>

图表的数据,来自控制器DisplayGraph的功能。控制器代码如下所示。

public ActionResult DisplayGraph(String InputStudent = "IP11")
{
    var query = (from b in db.gradesPerStudent
                 where b.Student.Equals(InputStudent)
                 orderby b.Date
                 select b);

    ViewBag.ChartEmpty = "No";
    if (query.ToList().Count == 0)
    {
        ViewBag.ChartEmpty = "Yes";
        ViewBag.Title = "No results for Student " + InputStudent;
        ViewBag.dates = null;
        ViewBag.grades = null;
        DateTime aminDate = new DateTime();
        ViewBag.minDate = aminDate.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
        return View();
    }

    DateTime minDate = query.Min(y => y.Date);
    DateTime maxDate = query.Max(y => y.Date);
    var dates = query.Select(x => EntityFunctions.DiffDays(query.Min(y => y.Date), x.Date));
    var grades = query.Select(x => x.grades);
    var datestable = dates.ToArray();
    var gradestable = grades.ToArray();
    List<int?> dateslist = new List<int?>();
    List<double> gradeslist = new List<double>();
    double result = dates.Count() * 1.0 / 70.0;
    int pointStep = (int)Math.Ceiling(result);
    for (int i = 0; i < dates.Count(); i = i + pointStep)
    {
            dateslist.Add(datestable.ElementAt(i));
            gradeslist.Add(gradestable.ElementAt(i));
    }
    ViewBag.dates = dateslist;
    ViewBag.grades = gradeslist;

    ViewBag.minDate = minDate.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;

    return View(query.ToList());

}

现在,当学生是一所学校的新学生时,我不想显示图表,而是显示该学生是新学生的消息,并且还没有该学生的统计数据。

所以我的问题是。我如何从我的控制器说什么时候应该执行 javascript,什么时候不应该执行?

非常感谢

【问题讨论】:

  • @ekad 非常感谢。这是一个非常好的主意。我认为蒂姆已经更新了他的答案。

标签: c# javascript asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

我如何从我的控制器中说何时应该执行 javascript 什么时候不?

通过视图模型。视图模型应该包含渲染视图所需的所有信息,并且视图应该包含您的脚本(或指向它的链接)。

尽量减少使用ViewBag,并首选强类型模型。

型号

public class MyViewModel {
    public bool ShowGraph { get; set; }

    public List<object> Data { get; set; }

    public List<int?> Dates { get; set; }

    public List<double> Grades { get; set; }
}

控制器

public ActionResult Index(){

    var model = new MyViewModel();
    model.ShowGraph = !IsStudentNew();

    // set your other properties here, instead of using the ViewBag

    return View( model );
}

查看

@model MyViewModel

@if( Model.ShowGraph ){
    <script>
        // javascript to render graph goes here
    </script>
}else{
    <div>No statistics yet.</div>
}

【讨论】:

  • 这看起来很不错。谢谢。我现在就试试。但是创建模型是最简单的方法吗?因为我的视图也已经与其他模型连接了......
  • 创建模型几乎总是正确的方法。看起来query.ToList() 是您当前的型号?您可以将该值设置为新视图模型的属性(例如 Data 属性)。
  • 嗯...非常感谢。 :)
【解决方案2】:

我只需要您模型的 GraphText 属性。将其显示在图表的顶部。如果有统计信息,则将此属性留空。如果没有..给它一个值:

public class YourModel {
    public IList<Statistic> StudentStatistics { get; set; }
    public string GraphText {
        get {
            return StudentStatistics.Any() ? "" : "No statistics yet";
        }
    }
}

然后在您的视图中,只需将GraphText 属性呈现在图表顶部即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多