1

My variable in the controller returns as null even after it's theoretically been set. Am I doing something very wrong?

Controller:

public class HomeController : Controller
{
    List<DataModel.MonthlyCount> table;

    List<DataModel.MonthlyCount> GetTableData(DateTime startDate, DateTime endDate)
    {
        table = DataManager.PopulateList(startDate, endDate);
        return table;
    }

    public ActionResult Monthly()
    {
        return View();
    }
    public PartialViewResult MonthlyTable(String startDate, String endDate)
    {
        DateTime sDate = DateTime.Parse(startDate);
        DateTime eDate = DateTime.Parse(endDate);
        if (table == null)
        {
            table = GetTableData(sDate, eDate);
            Debug.WriteLine("If you see this, it means table was null");
            Debug.WriteLine(table);
        }

        return PartialView("_MonthlyTable", table);
    }
    [HttpPost]
    public ActionResult ExcelExporter(String[] monthlySelections, String startDate, String endDate, String excelExporter)
    {
        DateTime sDate = DateTime.Parse(startDate);
        DateTime eDate = DateTime.Parse(endDate);
        int numMonths = ((sDate.Year - eDate.Year) * 12) + sDate.Month - eDate.Month;          

        if (table == null)
        {
            table = GetTableData(sDate, eDate);
        }
        ModelState.Clear();
        if (excelExporter == "true")
        {
            Workbook workbook = Data.MonthlyExcelExport.MaptoExcelTemplate(table, monthlySelections, numMonths);
            ExcelDownload(workbook, WorkbookFormat.Excel97To2003);
        }
        return View();
    }

View:

    <form id ="exportForm" name="exportForm" action ="Home/ExcelExporter" method ="post">
    <div class ="date-pick">
        <span>
           Start date <input type="text" id="start_date" name ="startDate" required=""/>
        </span> 
        <span>
            End date <input type="text" id="end_date" name ="endDate" required =""/>
        </span>
        <button type="button" id="updatePage" name="excelExporter" value="false" onclick="return clicked()">Update Page</button>
        <button type="submit" id ="excelExporter" name="excelExporter" value ="true" onclick="return clicked()">Export to Excel</button>
    </form>
<script>
$("#updatePage").click(function () {
    $.ajax({
        type: 'POST',
        datatype: 'html',
        data: $('#exportForm').serialize(),

        url: '../Home/MonthlyTable',
        success: function (data) {
            $('#_MonthlyTable').html(data);
        },
        error: function (data) {
            alert("failed");
        },
    });
});
</script>

I'm trying to make it so that I only ever have to run the GetTableData() method only once, regardless of if they press the Update Page (ajax call) button or the Export to Excel button, so as to only make a database call once.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Hackily
  • 208
  • 2
  • 8
  • 5
    The controller will be "new" each request. While using `static` may work in a single-app domain setup, *it is not reliable and will break in any non-trivialized deployment.* The key term to look for here is "caching" where the table will still be loaded each time from some persisted ("cached") store, but one that is significantly faster than the original, if such was slow to begin with. – user2864740 Jan 30 '15 at 16:26
  • 3
    The key terms to look up is Session State – John Saunders Jan 30 '15 at 16:28
  • @JohnSaunders touché – user2864740 Jan 30 '15 at 16:28
  • @user2864740: I'm not going to make an answer out of it so you go ahead. Make it a good answer and I'll upvote. – John Saunders Jan 30 '15 at 16:29
  • See http://stackoverflow.com/questions/23419011/is-there-a-best-practice-and-recommended-alternative-to-session-variables-in-mvc - if that doesn't give some solution approaches, I'm not sure what will (I couldn't find anything specific for this exact situation; overloaded terms makes it tough). However, if the original `DataManager.PopulateList` isn't *proven* to be too slow, just call it each time. – user2864740 Jan 30 '15 at 16:36
  • OK, I appreciate the help! :) – Hackily Jan 30 '15 at 17:29

0 Answers0