Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:52:30+00:00 2026-06-18T03:52:30+00:00

Possible Duplicate: Object reference not set to an instance of an object – Partial

  • 0

Possible Duplicate:
Object reference not set to an instance of an object – Partial View

In code below you can see my Controller Action code,View Page and model class. Every time when I run app I get error message: Object reference not set to an instance of an object. This message appears on View lint starts with foreach(var item in (IEnumerable....). How can I resolve this problem?

Controller:

public ActionResult Upload()
    {          

        var FilesInfoData = new List<FileInfoModel>(){

             new FileInfoModel(){Name = "sa",Length = 5, LastWriteTime = DateTime.Now},
             new FileInfoModel(){Name = "saa",Length = 5, LastWriteTime = DateTime.Now}

            };

        ViewData["FilesInfoView"] = FilesInfoData;


        return View();
    }

View:

  <%@ Control Language="C#"             Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<FileInfoModel>>" %>
  <%@ Import Namespace="MembershipTest.Models"%>
  <fieldset>
  <legend>
   Upload File:
  </legend>
  <% using (Html.BeginForm("Upload", "Home", FormMethod.Post, new {  @enctype="multipart/form-data"}))
  { %>
   <span>Filename:</span>
  <input type="file" name="file" id="file" />
  <input type="submit" value="Upload" />
  <% } %>

  </fieldset>
  <div id="uploadTable">
  <table border="0" cellpadding="0" cellspacing="0">
  <thead>
  <tr>
  <th>
  Filename
  </th>
  <th>
  Length
  </th>
  <th>
  LastModified
  </th>
  </tr>
  </thead>
  <tbody>

  <% 
  foreach (var item in ((IEnumerable<FileInfoModel>)ViewData["FilesInfoView"]))
  { %>
  <tr>
  <td>
  <%: item.Name %>
  </td>
  <td>
  <%: item.Length %>
  </td>
  <td>
  <%: item.LastWriteTime %>
  </td>
  </tr>
  <% } %>
  </tbody>
  </table> 
  </div>

Model:

public class FileInfoModel
{     
    public string Name { get; set; }
    public double Length { get; set; }
    public DateTime LastWriteTime { get; set; }       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:52:31+00:00Added an answer on June 18, 2026 at 3:52 am

    If your partial view is always looping through a collection of FileInfoModel in the ViewData, wouldn’t it be MUCH better to just set it’s model type to IEnumerable<FileInfoModel> and send that to the partial view?

    Also, you’ll need to put a null check on Model, as it seems that your collection is null at point of rendering the partial view.

    Like so:

    public ActionResult Upload()
    {          
        var model = new List<FileInfoModel>(){
    
             new FileInfoModel(){Name = "sa",Length = 5, LastWriteTime = DateTime.Now},
             new FileInfoModel(){Name = "saa",Length = 5, LastWriteTime = DateTime.Now}
            };
    
        return View(model);
    }
    

    Then at the top of your view, set the Model type:

    <% model IEnumerable<FileInfoModel> %>
    

    Then change your foreach:

    if (Model != null)
    {
        foreach (var item in Model)
        {
           //do stuff
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Object reference not set to an instance of an object errors when
Possible Duplicate: MVC3 Razor (Drop Down List) I've been getting Object reference not set
Possible Duplicate: What is a NullReferenceException in .NET? Object reference not set to an
Possible Duplicate: passing in object by ref With the code below, the output would
Possible Duplicate: what happens to an object in Java if you do not reference
Possible Duplicate: JavaScript losing “this” object reference with private/public properties Why does the second
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: How do I reference a javascript object property with a hyphen in
Possible Duplicate: Javascript: Object Literal reference in own key’s function instead of ‘this’ I
Possible Duplicate: How come a non-const reference cannot bind to a temporary object? This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.