In Razor view, how to check model property is null?
@if (Model.ImageName != null)
{
}
else
{
}
Home / DeveloperSection / Forums / In Razor view, how to check model property is null?
In Razor view, how to check model property is null?
@if (Model.ImageName != null)
{
}
else
{
}
Aryan Kumar
03-Jul-2023Sure, here are a few ways to check if a model property is null in a Razor view:
Using the
if
statement:Razor CSHTML
Using the
is
operator:Razor CSHTML
Using the
IsNullOrEmpty
method:Razor CSHTML
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 theis
operator will suffice. However, if you also need to check if the property is empty, then you should use theIsNullOrEmpty
method.Here is an example of how to use the
IsNullOrEmpty
method to check if a model property is null or empty:Razor CSHTML
Sunil Singh
11-Jul-2017Try this
hope it helps you