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 7957001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:10:06+00:00 2026-06-04T04:10:06+00:00

I am having a view with tied to a model: @model IEnumerable<AJA.Models.DB.Article> all I

  • 0

I am having a view with tied to a model:

@model IEnumerable<AJA.Models.DB.Article>

all I want to do is to loop through the model to do some logic:

@foreach (var item in Model)
{
    var article = item.article1;
    if (article.Length > 500)
    {
        article = article.Substring(0, 500) + "...";
    }

However, when I execute the View, error:

exception: System.NullReferenceException: The reference of the object is not defined 
to an instance of the object.

If I do @Html.DisplayFor(modelItem => item.article1) alone, I get the article OK, but I want to do business logic beforehand.

What’ s wrong with it ?

  • 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-04T04:10:08+00:00Added an answer on June 4, 2026 at 4:10 am

    First off in your View, triple check to make sure that you are using your ViewModel correctly.

    Something like this should be at the top of your View:

    @model IEnumerable<AJA.Models.DB.Article>
    

    Set a breakpoint in your controller and use F10 to drill down all the way to your View, you will be able to inspect the objects in your View’s foreach loop.

    Somewhere along the line an object is null where you expect it to have something. Figure out what that object is.

    @foreach (var item in Model)
    {
        var article = item.article1; //article1 may be null. Check it!
        if (article.Length > 500)
        {
            article = article.Substring(0, 500) + "...";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a view having two layers on it. Then i want
How do I go about having two view models communicate with one another using
I'm having some serious trouble understanding the view matrix in XNA. I've come pretty
Consider a web page having grid-view connected to SqlDataSource having all permission to insert
Is there a way to render a html page without having a view model
I am having an Activity in which I am having some View that is
Well I have a view of this type of hierarchy Main View (having some
I want to make a custom List View having Two TextViews and a radio
I an having list view of Image and i want something like...when user long
I am having table view. I want to dynamically increase the height of the

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.