0

I am trying to save list of object from view to controller but i am getting NullReferenceException when list is more that 25. It works fine if list less than 25.

public async Task<IActionResult> ImportStudentExcel(IFormFile file)
{
 var list = new List<StudentImport>();
//Here it contains logic for adding item to list from excel file
ViewBag.FileName = file.FileName;
return View(list.ToList());
}

I am getting all the item in my view I am doing this to bind properties

  //Containes Table for Showing List
  <form id="saveForm" asp-action="SaveFromImport" asp-controller="StudentImport" method="POST">
  <input type="hidden" name="filename" value="@ViewBag.FileName">
  @for(int i=0; i<Model.Count; i++)
      {
        <input asp-for="@Model[@i].Fullname" type="hidden" value="@Model[@i].Fullname"/>
        <input asp-for="@Model[@i].Gender" type="hidden" value="@Model[@i].Gender"/>
        <input asp-for="@Model[@i].DOB" type="hidden" value="@Model[@i].DOB"/>
         // Other 15 Properties Like Address, PhoneNumber, RegNo etc
      }
    <input type="submit" value="Save">
   </form>

When I inspect this page all item are present

public async Task<IActionResult> SaveFromImport(List<StudentImport> students, string filename)
        {
            try
            {
                foreach (var t in students)
                {
                   
                    System.Console.WriteLine(t.Fullname);
                    //Save to DB
                }
             }
            catch (Exception e)
            {
                System.Console.WriteLine(e.ToString());
                

            }
            return RedirectToAction("Index", "Student");
        }

Am getting NullReference at foreach Statement. I dont know whats going on. It works as expected when list count is 13 but wont work when count is 25 or more, It also works when there is only one property in StudentImportModel and count is sttil 25.

zombie551
  • 108
  • 12
  • 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) – derpirscher Sep 26 '20 at 09:20
  • NO. in my case, the list is not null when there is fewer data.. but it is null when there is more data.. – zombie551 Sep 26 '20 at 09:54

1 Answers1

0

The ExceptionMessage in my case was NullReferenceException but the actual error was InvalidDataException: Form value count limit 1024 exceeded. However, I managed to solve this by adding this code in ConfigureServices method.

 services.Configure<FormOptions>(options =>
    {
        options.ValueCountLimit = 6000;
    });
zombie551
  • 108
  • 12