0

Hi I am getting something wrong when trying to pass my model from the controller to the view and i m not certain what it is:

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

Partial View 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>

I am getting an error on the Model.InterRecordList as it thinks it is null - I have checked the controller and the model is being populated properly butby the time its reached the view it has lost all its vallues.

I'm sure I am missing something obvious but any help would be much appreciated!

1 Answer 1

4

It appears you are accidentally creating a new object:

PartialView(new InterReportJson());

You probably mean to use the existing one: PartialView(interReportJson);

Sign up to request clarification or add additional context in comments.

1 Comment

how stupid! thanks i ll mark as an answer in 4 mins! thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.