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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:25:22+00:00 2026-05-21T15:25:22+00:00

I have an MVC razor view that iterates over an Orders collection. Each order

  • 0

I have an MVC razor view that iterates over an Orders collection. Each order has a Customer, which can be null.

Trouble is, I get a null reference exception when this is the case.

@foreach (var item in Model) {
<tr>
        <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
    <td>
        @item.Number
    </td>
    <td>
        @String.Format("{0:g}", item.ReceivedDate)
    </td>
    <td>
        @item.Customer.Name
    </td>

@item.Customer.Name blows up when item.Customer is null (as you’d expect).

This must be an easy question but haven’t been able to find the answer!

What’s the best way to deal with this, without setting up a ViewModel ?

Thanks
Duncan

  • 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-05-21T15:25:22+00:00Added an answer on May 21, 2026 at 3:25 pm

    A simple if should do the job:

    <td>
        @if (item.Customer != null)
        {
            <text>@item.Customer.Name</text>
        }
    </td>
    

    This being said and shown, that’s only a workaround. The real solution consists in defining and using a specific view model.

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

Sidebar

Related Questions

I have MVC 1.0 app on VS2008. I have webpage that has a search
I'm playing around with Razor + MVC 3 and have a really simple scenario...
I'm using ASP.NET MVC 3 (Razor) and have created a Controller named Controllers -
I am using Composite C1 cms, mvc and razor view engine. Some page should
I'm using MVC 3 with the Razor view engine and I would like to
I'm using the Razor view engine with MVC 3 and I'm trying to make
I recently joined a team that is working on a ASP.NET MVC project.. they
Do asp.net aspx views have tags that that work similar to the Ruby erb
I have MVC 1.0 app with VS2008. I have added configuration to web.config but
I have MVC 2 site, and I.E is cacheing word documents linked on my

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.