Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:57:40+00:00 2026-06-17T05:57:40+00:00

I am very new to ASP.NET MVC so this would probably be an easy

  • 0

I am very new to ASP.NET MVC so this would probably be an easy question. I followed the W3 Schools Movies tutorial and created a small database with fields Naslov (“Heading” in my language) Sadrzaj (“Content” in my language) and Datum (“Date”) which I want to use as blog posts and to list on main Index. This is my current code for Aktuelnosti.cs

    namespace comp_2000.Models
{
    public class Aktuelnosti
    {
        public int ID { get; set; }
        public string Naslov { get; set; }
        public string Sadrzaj { get; set; }
        public DateTime Datum { get; set; }
    }
    public class AktuelnostiContext : DbContext
    {
        public DbSet<Aktuelnosti> Aktuelnosti { get; set; }
    }
}

And this is my related view for Index.cshtml in Aktuelnosti, which works

@model IEnumerable<comp_2000.Models.Aktuelnosti>

<table border="1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Naslov)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sadrzaj)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Datum)
        </th>
        <th></th>
    </tr>


        @foreach (var item in Model) {
    <tr>
        <td width="20%">
            @Html.DisplayFor(modelItem => item.Naslov)
        </td>
        <td width="60%">
            @Html.DisplayFor(modelItem => item.Sadrzaj)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Datum)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Preview", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>

So, when I try the same code for Index.cshtml inside Home, which looks like this

@model IEnumerable<comp_2000.Models.Aktuelnosti>
@foreach (var item in Model)
{
<p>@Html.DisplayFor(modelItem => item.Naslov)</p>
<p>@Html.DisplayFor(modelItem => item.Sadrzaj)</p>
<p>@Html.DisplayFor(modelItem => item.Datum)</p>
<hr>
}

I get this

 Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution 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.

Source Error: 


Line 48:       
Line 49: 
Line 50: @foreach (var item in Model)
Line 51:     {
Line 52:     <p>@Html.DisplayFor(modelItem => item.Naslov)</p>

It says the problem is in foreach loop. What I am doing wrong? I just want the same data from one database displayed on two different pages.

Edit: There is also something about NullReferenceException that was unhandled by the user code. Hope this is of any use for you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:57:41+00:00Added an answer on June 17, 2026 at 5:57 am

    System.NullReferenceException means that something is null. In this case, most likely the Model is null. Make sure that the controller is passing the Model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to ASP.NET MVC, so forgive me if this is something I
I am relatively new to ASP.NET MVC, and am very impressed with the clarity
I've recently started tinkering with ASP.NET MVC, but this question should apply to classic
I am very new in MVC trying to expand my asp.net knowledge after almost
If you were starting a new web development project would you use ASP.Net MVC
I'm very new to ASP.NET MVC (and .NET in general). I'm trying to throw
I'm kind of new to the .NET platform. And currently I'm learning ASP.NET MVC.
I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()
I'm a bit new to ASP.NET MVC and I'm wondering if there is a
I am working just one week with ASP.NET MVC 3. This might be a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.