---
title: "In Razor view, how to check model property is null?"  
description: "In Razor view, how to check model property is null?"  
author: "Simond Gear"  
published: 2017-07-10  
updated: 2023-07-03  
canonical: https://www.mindstick.com/forum/34319/in-razor-view-how-to-check-model-property-is-null  
category: ".net"  
tags: [".net", "mvc"]  
reading_time: 2 minutes  

---

# In Razor view, how to check model property is null?

In [Razor view](https://www.mindstick.com/forum/155820/how-to-generate-textbox-control-using-htmlhelper-in-razor-view), how to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) [model](https://yourviews.mindstick.com/view/81334/america-is-reopening-after-corona-lockdown-but-on-swedish-model) [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) is [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp)?

```
       @if
(Model.ImageName != null)            {            }            else            {             }
```

I am [writing](https://www.mindstick.com/articles/12997/5-essential-tips-on-resume-writing-for-business-analysts) this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) but it is not working. Please help me.

## Replies

### Reply by Aryan Kumar

Sure, here are a few ways to check if a model property is null in a [Razor](https://www.mindstick.com/articles/12002/asp-dot-net-mvc5-razor-with-jquery) [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view):

**Using the** `if` **statement:**

Razor CSHTML

```plaintext
if (Model.Property != null)
{
    // Do something with the property
}
else
{
    // The property is null
}
```

**Using the** `is` **operator:**

Razor CSHTML

```plaintext
if (Model.Property is string)
{
    // The property is a string
}
else
{
    // The property is null or not a string
}
```

**Using the** `IsNullOrEmpty` **method:**

Razor CSHTML

```plaintext
if (string.IsNullOrEmpty(Model.Property))
{
    // The property is null or empty
}
else
{
    // The property is not null or empty
}
```

Which method you use will depend on your specific needs. For example, if you only need to check if the property is null, then the `if` statement or the `is` operator will suffice. However, if you also need to check if the property is empty, then you should use the `IsNullOrEmpty` method.

Here is an example of how to use the `IsNullOrEmpty` method to check if a model property is null or empty:

Razor CSHTML

```plaintext
@if (string.IsNullOrEmpty(Model.Name))
{
    // The name property is null or empty
}
else
{
    // The name property is not null or empty
}
```

### Reply by Sunil Singh

Try this \

\

```
<div id="divLoading">       @if (Model != null && !String.IsNullOrEmpty(Model.Record)){    <label for="record">add record</label>}else{     <label for="remove">remove</label>}     </div>
```

hope it helps you\


---

Original Source: https://www.mindstick.com/forum/34319/in-razor-view-how-to-check-model-property-is-null

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
