0

嗨,当我尝试将我的模型从控制器传递到视图时出现问题,我不确定它是什么:

控制器(StatsController):

  [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult InterStats(int manufacturerId, int countryId, DateTime startDate, DateTime endDate)
    {

        //Get all manufacturerwidgets for manufacturer
        var manufacturerWidget = _manufacturerWidgetsRepository.GetManufacturerWidgetByManufacturerAndCountry(manufacturerId, countryId);
        var interReportJson = new InterReportJson();
        var interRecordList = new List<InterRecord>(); // a list of my anonymous type without the relationships
        interReportJson.InterRecordList = new List<InterRecord>();
        var count = 1;
        foreach (var mw in manufacturerWidget)
        {
            var widgetName = mw.Description;

            //Get the product stats data
            var imps = _productStatsRepository.GetSumImpressionsProductStatsForManufacturerCountryDate(
                mw.Id, countryId, startDate, endDate);


            var clicks = _productStatsRepository.GetSumClicksProductStatsForManufacturerCountryDate(
                mw.Id, countryId, startDate, endDate);

            float ctr = 0;
            if (imps != 0 && clicks != 0)
            {
                ctr = ((clicks / (float)imps) * 100);
            }



            //  Create the data for the report
            var interRecord = new InterRecord
            {
                WidgetName = widgetName,
                Impressions = imps,
                Interactions = clicks,
                Ctr = ctr,
                Count = count
            };




           interReportJson.InterRecordList.Add(interRecord);

            count++;
        }

        interReportJson.Counter = count;




        return PartialView(new InterReportJson());
    }

部分视图 InterStats.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dashboard.e_taleiq.com.InterReportJson>" %>


<script type="text/javascript">

                        //see this http://stackoverflow.com/questions/11472947/how-to-format-my-json-data-for-stack-column-chart-in-highcharts


                        var widgetNameArray = [];

                        var impressionsArray = [];

                        var intsArray = [];


                       <%  foreach(var item in Model.InterRecordList)
                            {%>

                                // var item1 = data[i];
                                //only display on graph if not 0
                                if (<%: item.Impressions %> > 0) {


                                    var widgetCategories = <%: item.WidgetName %>;

                                    //put into an array
                                    widgetNameArray.push(widgetCategories);

                                    var imps = <%: item.Impressions %>;

                                    impressionsArray.push(imps);

                                    var ints = <%: item.Interactions %>;
                                    intsArray.push(ints);
                                }
                           <%  }%>



                        // Create the chart
                        $('#container').highcharts({
                            chart: {
                                type: 'column'
                            },
                            title: {
                                text: 'Inter Chart ' + startDate + ' to ' + endDate
                            },
                            xAxis: {
                                categories: widgetNameArray,
                                labels: {
                                    rotation: -45,
                                    align: 'right',
                                    style: {
                                        fontSize: '13px',
                                        fontFamily: 'Verdana, sans-serif'
                                    }
                                }
                            },
                            yAxis: {
                                min: 0,
                                title: {
                                    text: 'Impressions/Interactions'
                                },
                                stackLabels: {
                                    enabled: false,
                                    style: {
                                        fontWeight: 'bold',
                                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                                    }
                                }
                            },
                            legend: {
                                align: 'right',
                                x: -100,
                                verticalAlign: 'top',
                                y: 20,
                                floating: true,
                                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                                borderColor: '#CCC',
                                borderWidth: 1,
                                shadow: false
                            },
                            tooltip: {
                                formatter: function () {
                                    return '<b>' + this.x + '</b><br/>' +
                        this.series.name + ': ' + this.y + '<br/>';
                                }
                            },
                            plotOptions: {
                                bar: {
                                    dataLabels: {
                                        enabled: true
                                    }
                                }
                            },
                            series: [{
                                name: 'Impressions',
                                data: impressionsArray
                            }, {
                                name: 'Interactions',
                                data: intsArray
                            }]
                        });




                        var table = document.getElementById("usertable");
                        var tabledata = "";

                        tabledata += "<tr>";
                        tabledata += "<th>Widget Name</th>";
                        tabledata += "<th>Impressions</th>";
                        tabledata += "<th>Interactions</th>";
                        tabledata += "<th>CTR</th>";
                        tabledata += "</tr>";



                       <%  foreach(var itemTable in Model.InterRecordList)
                            {%>

                            tabledata += "<tr>";
                            tabledata += "<td>" +  <%: itemTable.WidgetName %> + "</td>";
                            tabledata += "<td>" +  <%: itemTable.Impressions %> + "</td>";
                            tabledata += "<td>" +  <%: itemTable.Interactions %> + "</td>";
                            tabledata += "<td>" +  <%: itemTable.Ctr %>.toFixed(2) + "%</td>";
                            tabledata += "</tr>";

                            <%  }%>



                        table.innerHTML = tabledata;

                        $("th").css("background-color", "#3399FF");
                        $("tr:even").css("background-color", "#eeeeee");
                        $("tr:odd").css("background-color", "#ffffff");








</script>

我在 Model.InterRecordList 上遇到错误,因为它认为它为空 - 我已经检查了控制器并且模型正在正确填充,但是当它到达视图时它已经失去了所有的价值。

我确定我遗漏了一些明显的东西,但任何帮助将不胜感激!

4

1 回答 1

4

看来您不小心创建了一个新对象:

PartialView(new InterReportJson());

您可能的意思是使用现有的:PartialView(interReportJson);

于 2013-06-28T14:45:10.427 回答