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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:03:26+00:00 2026-05-30T16:03:26+00:00

All too often I’ve run into a situation in which a view in my

  • 0

All too often I’ve run into a situation in which a view in my project throws a null reference exception.

@model Johnny.Application.TestModel
<div>@(Model.SomeText)</div>

This throws an error if Model is null.

But how are people handling this? I certainly don’t see code samples everywhere with ugly null checks littering the code in the view. That leads me to believe that most of the time, controllers aren’t supposed to return null models. But how can you enforce this with more finesse?

Right now as soon as someone accidentally causes a controller to return a null model, the view model blows up and looks to be at fault. In reality, it was the controller’s fault. And the view may not even “catch” the problem, it will only do so if the model’s members happen to get used (which is most of the time, of course).

For various reasons, some views may want to handle null values. I wouldn’t expect this to be the majority case, though. Clearly this is the matter of setting some “contract” between view and controller.

I don’t like the options I’ve seen:

  1. Check if model is null every time it’s used. Very lame!
  2. One big if statement wrapping the whole view with a null model
    check. Think of the wasted code real estate. Lame!
  3. Add an if check with a throw at the top. Not bad, but seems silly. Mildly lame.

I would love to know if something like these options existed to set the “no nulls” contract:

  • An attribute on the controller method like [NoNullModels]. I doubt this exists, since I don’t think the controller knows what view it is hooking up to.
  • In the view, an indicator like @MVC3.HeyDontAllowNulls or some other standard way of throwing an exception (like option 3 above)
  • 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-30T16:03:26+00:00Added an answer on May 30, 2026 at 4:03 pm

    I asked a similar question here Should one try to guard against null reference exceptions / index out of bounds exceptions in MVC views? and got good responses to it. In short, it is preferred to add null checks in your controllers and perhaps even unit tests rather than in your views.

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

Sidebar

Related Questions

All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of
I tend to take the academic approach all too often and adhere to strict
I run my blog using Wordpress and all too recently became a big believer
All too often I find myself storing object data in hidden dom elements. I
We're considering switching our site from Prototype to jQuery. Being all-too-familiar with Prototype, I'm
I would like to customize the background (and maybe the border too) of all
I've been reading StackOverflow too much and started doubting all the code I've ever
There seems to be too many attributes/parameters in CSS... I want to know all
Good afternoon, all. I'm not too hopeful for a yes here, but if anyone
I am (too) often confronted with the task of having to parse textual data

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.