2

Please see the Error section below. Other related code is as follows:

Model:

public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }

BlogPostController

public IActionResult Index()
        {
            return View("~/Views/BlogsTest/Index.cshtml");
        }

View [Index.cshtml]:

@model IEnumerable<ASPCoreCodeFirstApp.Models.Blog>

<p>
    <a asp-controller="BlogPost" asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Url)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Url)
                </td>
                <td>
                    <a asp-controller="BlogPost" asp-action="Edit" asp-route-id="@item.BlogId">Edit</a>
                </td>
                <td>
                    <a asp-controller="BlogPost" asp-action="Create" asp-route-id="@item.BlogId">Edit</a>
                </td>
            </tr>
        }
    </tbody>
</table>

Error at @foreach (var item in Model)....line:

I can see the Index method gets called and when I put the breakpoint at the @foreach (var item in Model) line of Index.cshtml file I get Model as null and the browser throws the following error. Blogs table does have data. Even if it did not have data the foreach loop should have returned empty result instead of an NULL exception error. The view relative path in the Index action method seems write since Index.cshtml view does get called. What I may be missing?:

NullReferenceException: Object reference not set to an instance of an object.
MoveNext in Index.cshtml, line 22

Folder Hierarchy:

Model above is in Model.cs file highlighted below:

enter image description here

nam
  • 21,967
  • 37
  • 158
  • 332
  • You don't send a model to your view from your controller action like this : return View("ViewName", model ) . You must at least send an empty list of Blog objects : return View("...", new List()); – x82 Jul 20 '16 at 03:56
  • @x82 Thank you for pointing out my mistake. It's working now. You may want to change your comment to an answer and I will mark it as an answer. You may want to add a little explanation of the error for the benefit of other users. – nam Jul 20 '16 at 04:10

1 Answers1

5

Your view is a strongly typed view requiring a list (to be precise, IEnumerable) of Blog objects :

@model IEnumerable<ASPCoreCodeFirstApp.Models.Blog>  

For strongly typed views, you have to pass an object of the type that the view requires from your controller action for the view engine to be able to render the view ( Even if it's just an empty list ) :

View("ViewName", new List<Blog>())
x82
  • 216
  • 1
  • 4