0

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

faseeh
  • 131
  • 10
  • can you share your model and controller? – Udara Kasun Sep 04 '18 at 14:39
  • 1
    i'll Edit my Question once again – faseeh Sep 04 '18 at 14:41
  • The issue won't be the view but it will be in your controller. For example, what is your view returning? `public IActionResult storeList() { var list = _context.UkTable; return View(list); }` – theJ Sep 04 '18 at 14:43
  • 1
    [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) – Magnus Sep 04 '18 at 14:44
  • im returning view() as i have done in the past with the same logic and it works fine but now i got this error in my new project – faseeh Sep 04 '18 at 14:45
  • 3
    you sholud set model View(yourmodel); like this – Udara Kasun Sep 04 '18 at 14:45
  • You are using .cshtml. Just put a breakpoint in your .cshtml file and monitor where it breaks and which value is NULL. – Matt Sep 04 '18 at 14:46
  • udara kasu i checked but still the error is same – faseeh Sep 04 '18 at 14:51
  • 1
    matt the things is i created my table obj in the controller to check if im getting data from table or not and it's not getting any value. – faseeh Sep 04 '18 at 14:52
  • how to set list to your model? – Udara Kasun Sep 04 '18 at 14:52
  • 1
    udara kasun view(db.mymodel.tolist()); this is what i had to do i did and it's working fine. thanks mate – faseeh Sep 04 '18 at 14:55
  • 1
    @faseeh a better way to code is to use my initial suggestion. Create the your variable for your model `var myModel = db.mymodel.tolist();` and then add the `myModel` varibale to your view `view(myModel);` – theJ Sep 04 '18 at 15:01
  • 1
    Possible duplicate of [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) –  Sep 04 '18 at 22:46

1 Answers1

2

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>
Udara Kasun
  • 1,825
  • 12
  • 21