从模型中获取所有项目(asp.net mvc)

时间:2016-02-01 10:27:55

标签: javascript asp.net asp.net-mvc model

    $("#collectorscompleted").kendoChart({
        legend: {
            visible: false
        },
        seriesDefaults: {
            type: "column"
        },
        series: [{
            name: "Completed",
            data: [@Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID==1), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 2), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 3), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 4),
                   @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 5), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 6), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 7), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 8),
                   @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 9), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 10), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 13), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 14),
                   @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 15), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 16), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 17), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 18)]
        }, {
            name: "Failed",
            data: [@Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID==1), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 2), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 3), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 4),
                   @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 5), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 6), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 7), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 8),
                   @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 9), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 10), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 13), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 14),
                   @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 15), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 16), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 17), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 18)]
        }],
        valueAxis: {
            labels: {
                visible: false
            },
            line: {
                visible: false
            },
            majorGridLines: {
                visible: false
            }
        },
        categoryAxis: {
            categories: [1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18],
            line: {
                visible: false
            },
            majorGridLines: {
                visible: false
            }
        },
        chartArea: {
            background: "none"
        },
        tooltip: {
            visible: true,
            format: "{0}",
            template: "#= series.name #: #= value #"
        }
    });

我有以下脚本。然而,类别现在很难在我自己输入,这是错误的 我的问题是:如何从模型中获取所有CollectorID

分类:[1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18],//这里所有的收藏家ID都是

1 个答案:

答案 0 :(得分:0)

您正在使用MVC。要从模型中获取collectorIds,必须将其添加到控制器中。在调用此初始化程序之前,您必须在ajax中调用控制器。这看起来像这样

Javascrit

function GetCollectionInformationAjax()
    {
        $.ajax({
            type: "GET",
            url: "@Url.Action("GetCollectionInformation", "YourController")",
            contentType: "application/json",
            cache: false,
            success: function (data) {
                //call the initialzer here
                Initialize(data);
            },
            error: function (xhr, status, error) {

            }
        });
    }

C#你的控制器

public actionResult GetCollectionInformation()
{
    //This is an exemple, you have to replace it!
    return [1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18];
}

你的javascript

function Initialize(data)
{
$("#collectorscompleted").kendoChart({
    legend: {
        visible: false
    },
    seriesDefaults: {
        type: "column"
    },
    series: [{
        name: "Completed",
        data: [@Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID==1), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 2), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 3), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 4),
               @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 5), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 6), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 7), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 8),
               @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 9), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 10), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 13), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 14),
               @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 15), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 16), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 17), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 18)]
    }, {
        name: "Failed",
        data: [@Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID==1), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 2), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 3), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 4),
               @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 5), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 6), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 7), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 8),
               @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 9), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 10), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 13), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 14),
               @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 15), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 16), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 17), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 18)]
    }],
    valueAxis: {
        labels: {
            visible: false
        },
        line: {
            visible: false
        },
        majorGridLines: {
            visible: false
        }
    },
    categoryAxis: {
        categories: data,
        line: {
            visible: false
        },
        majorGridLines: {
            visible: false
        }
    },
    chartArea: {
        background: "none"
    },
    tooltip: {
        visible: true,
        format: "{0}",
        template: "#= series.name #: #= value #"
    }
});}