MVC从控制器传递模型来查看错误

时间:2013-06-28 14:42:36

标签: c# asp.net-mvc-3

您好我在尝试将模型从控制器传递到视图时出错了,我不确定它是什么:

Controller(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上收到一个错误,因为它认为它是空的 - 我已经检查了控制器并且模型正在正确填充但是当它到达视图时它已经丢失了所有的vallues。

我确信我错过了一些显而易见的东西,但任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:4)

您似乎无意中创建了一个新对象:

PartialView(new InterReportJson());

您可能想要使用现有的:PartialView(interReportJson);