When trying to get data from a list (created by an excel spreadsheet) in my HomeController, I run into an issue where the data is generated, but I'm unable to produce it onto a webpage. If I try to generate it using the model from the page directly, I get System.NullReferenceException: 'Object reference not set to an instance of an object.'
With out it even loading to the initial page.
My HomeController (Removed some items from the list for demonstration of issue):
public IActionResult Index()
{
return View();
}
public async Task<List<DataForProactiveContact>> Import(IFormFile file)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var list = new List<DataForProactiveContact>();
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
using (var package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
var rowcount = worksheet.Dimension.Rows;
for (int row = 2; row < rowcount; row++)
{
list.Add(new DataForProactiveContact
{
regionID = worksheet.Cells[row, 1].Value.ToString().Trim(),
storeID = worksheet.Cells[row, 2].Value.ToString().Trim(),
productStatus = worksheet.Cells[row, 3].Value.ToString().Trim(),
});
}
}
}
return list;
}
My Model:
public class DataForProactiveContact
{
public string regionID { get; set; }
public string storeID { get; set; }
public string productStatus { get; set; }
}
And my HTML:
@model IEnumerable<DataForProactiveContact>
@{
ViewData["Title"] = "Home Page";
}
<h2>Test Run</h2>
<body>
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="6000">
<div class="container">
<form method="post" asp-controller="Home" asp-action="Import" enctype="multipart/form-data">
<input type="file" accept=".xls,.xlsx" name="file" />
<button type="submit">Import Excel Form</button>
</form>
</div>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Region</th>
<th>Store</th>
<th>Product Status</th>
</tr>
@foreach (var p in ViewBag.DataForProactiveContact)
{
<tr>
<td>@p.regionID</td>
<td>@p.storeID</td>
<td>@p.productStatus</td>
</tr>
}
</table>
</body>
If I run the webpage with out the @foreach statement, I get the data generated and sent to a source data page called Import where my data is shown, rather than in a webpage.
My Goal is to get the information I have from the Model/List variable, put into the webpage and displayed as a table.
Any help would be greatly appreciated.
Dependencies are EPPlus NonCommercial. Running Firefox.