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.