1

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; }       
}
Community
  • 1
  • 1
user699503
  • 35
  • 6

1 Answers1

4

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
    }
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Can you write some shot example for that? – user699503 Feb 01 '13 at 12:25
  • @user699503 Check my edit, I've added an example. – Mathew Thompson Feb 01 '13 at 12:27
  • Ok that is exactly what i want but need just one small help. When I put all this in model as you recommend, this my partial view not open in aspx page where I call it than on new page (Upload.ascx). On aspx page i call it on this way:`<%= @Html.Partial("Upload") %>` – user699503 Feb 01 '13 at 12:34
  • You need to pass the partial view the model, like so: `<%= Html.Partial("Upload", Model) %>` (and take off that `@`, you're not using Razor) – Mathew Thompson Feb 01 '13 at 12:35
  • OK I do it but my Upload.aspx still open as separate page not on Index.aspx where I call it – user699503 Feb 01 '13 at 12:41
  • @user699503 I'm not sure what you mean by it "opens on a separate page". Does it open in a popup or something? – Mathew Thompson Feb 01 '13 at 12:43
  • Ok now I will explain: On index.aspx i call partial view Upload. After upload file (post inside partial view) I see only Upload.ascx but I want to see Upload.ascx inside Index.aspx. Do i need use ajax and update div tag fot this or what – user699503 Feb 01 '13 at 12:49
  • Change it to `return PartialView(model)` in your`Upload` action method – Mathew Thompson Feb 01 '13 at 12:51
  • It can't help me because after posting file I can't get Index page with this partial view. Before post my url is localhost/Home/Index, after posting file it is localhost/Home/Upload but I want to use Upload partial view as part of my Index page (part wich is responisble for uploading files and on rest of Index page I have some other fields) – user699503 Feb 01 '13 at 13:01