ASP.Net MVC3 C# query regarding viewing data stored in a database

I have a question regarding the MVC3 C# Tutorial.

When creating Controller, it automatically creates the Views for you, and Index.cshtml is created (path being /Views/News/Index.cshtml). This code looks like the below:

@model IEnumerable<dale_harrison.Models.News>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            News_Entry
        </th>
        <th>
        News_Date
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.News_Entry)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.News_Date)
        </td>
    </tr>
}
</table>

This works fine, however, I would like to display the contents of @Html.DisplayFor(modelItem => item.News_Entry) and @Html.DisplayFor(modelItem => item.News_Date) on my homepage, which is in a different View.

I tried to copy the code from Index.cshtml into a new view (path being /Views/Home/homepage.cshtml), but it fails with the error: "NullReferenceException was unhandled by the user code" - sorry, but I'm unsure what this means?

Would someone mind explaining why simply copying the code to display data from the database from one View to another View doesn't work? Apologies for being thick, but I'm a beginner with no coding background.

I can post the Controller code if required.

Many thanks.

Answers


maybe you omitted passing model to view when calling it.you can do this like :

public ActionResult HomePage()
{
    IEnumerable<dale_harrison.Models.News> model = new List<dale_harrison.Models.News>();  
    // prepare your model (populate it with proper data)

    return View(model);
} 

Need Your Help

indentation error while rendering template with django jinja

django django-templates django-views

I have this template in django 1.8 which gives me an indentation error :