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; }
}