Reputation: 131
I used the "List View" in the Controller to generate an storeList.cshtml file, within this view file, there is a line:
foreach (var item in Model) {}
When I run the app, this line keeps getting the "Nullreference" error, and the "Object reference not set to an instance of an object" error.
I'm confused because:
1) This view file is auto-generated; so why is it not correct?
2) I've used the exact same way ("List View") and generated many other view files, and never encountered this problem.
<table class="uk-table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CategoryID)
</th>
<th>
@Html.DisplayNameFor(model => model.typeID)
</th>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.storeUrl)
</th>
<th>
@Html.DisplayNameFor(model => model.storedes)
</th>
<th>
@Html.DisplayNameFor(model => model.tags)
</th>
<th>
@Html.DisplayNameFor(model => model.image)
</th>
<th>
@Html.DisplayNameFor(model => model.metaTitle)
</th>
<th>
@Html.DisplayNameFor(model => model.metaDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.popular)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryID)
</td>
<td>
@Html.DisplayFor(modelItem => item.typeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.storeUrl)
</td>
<td>
@Html.DisplayFor(modelItem => item.storedes)
</td>
<td>
@Html.DisplayFor(modelItem => item.tags)
</td>
<td>
@Html.DisplayFor(modelItem => item.image)
</td>
<td>
@Html.DisplayFor(modelItem => item.metaTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.metaDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.popular)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
Controller
public ActionResult storeList()
{
return View();
}
Model
public partial class store
{
public store()
{
this.Reviews = new HashSet<Review>();
}
public int id { get; set; }
public Nullable<int> CategoryID { get; set; }
public Nullable<int> typeID { get; set; }
public string name { get; set; }
public string storeUrl { get; set; }
public string storedes { get; set; }
public string tags { get; set; }
public string image { get; set; }
public string metaTitle { get; set; }
public string metaDescription { get; set; }
public Nullable<bool> popular { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
public virtual Type Type { get; set; }
}
Error
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the exec ution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
let me know how to fix this?
Thanks
Upvotes: 0
Views: 4619
Reputation: 2216
you can change this as you want.
in Model
public partial class store
{
public int id { get; set; }
public Nullable<int> CategoryID { get; set; }
public Nullable<int> typeID { get; set; }
public string name { get; set; }
public List<store> MyStoreList {get;set;}
}
in Controller
public ActionResult storeList()
{
store obj=new store();
obj.MyStoreList = new List<store>();// Load your list using uery
return View(obj);
}
in cshtml
@model projectname.Models.store
@foreach (var item in Model.MyStoreList) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryID )
</td>
<td>
@Html.DisplayFor(modelItem => item.typeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
</tr>
Upvotes: 2