0

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.

ImNotPsychotic
  • 47
  • 1
  • 10
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jazb Jan 02 '22 at 08:15
  • @Jazb Unfortunately not, I understand the reason why the item returns a Null. I'm just not understanding how to get data from one page to another. – ImNotPsychotic Jan 02 '22 at 08:43
  • Does you HTML represents index view? – Adlorem Jan 02 '22 at 10:50

1 Answers1

1

you have an index view, but don' t have any data. Model should be added

public IActionResult Index()
 {
    List <DataForProactiveContact> model = ...your code to fill list
     return View(model);
}

and fix the view

@if( Model != null)
{
@foreach (var p in Model.DataForProactiveContact)
 {
                <tr>
                    <td>@p.regionID</td>
                    <td>@p.storeID</td>
                    <td>@p.productStatus</td>
                </tr>
 }
}
Serge
  • 40,935
  • 4
  • 18
  • 45