c# - 从 Controller 控制我的 asp View 中的 javascript 代码是否将被执行

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

我正在开发 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>

图形的数据,来自 Controller DisplayGraph的函数。 Controller 的代码如下所示。

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

}

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

所以我的问题是。我如何从我的 Controller 中判断何时应该执行 javascript 以及何时不应该执行?

非常感谢

最佳答案

How do I say from my controller when a javascript should be executed and when not?

通过 View 模型。 View 模型应包含呈现 View 所需的所有信息,并且 View 应包含您的脚本(或指向它的链接)。

尽量减少使用 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; }
}

Controller

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>
}

关于c# - 从 Controller 控制我的 asp View 中的 javascript 代码是否将被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494090/

相关文章:

c# - 从设置中将文本 block 内容设置为字符串

c# - 我应该使方法静态

javascript - 使用 jquery 检查元素是否聚焦(周围有轮廓)

javascript - 从 CommonJS TypeScript 导入 es 模块文件

javascript - Java 脚本未在我的应用程序中执行

c# - 手机ocr示例代码

c# - ASP.NET MVC5 路由到 View 上的 div

asp.net - OpenIdConnect 错误 - 租户标识符可能不是空的 GUID

javascript - 添加了 javascript,现在链接不起作用

c# - 有什么方法可以确定 asp.net 中 CultureInfo 的文本方向吗?