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
ifstatement:Razor CSHTML
Using the
isoperator:Razor CSHTML
Using the
IsNullOrEmptymethod: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
ifstatement or theisoperator will suffice. However, if you also need to check if the property is empty, then you should use theIsNullOrEmptymethod.Here is an example of how to use the
IsNullOrEmptymethod to check if a model property is null or empty:Razor CSHTML
Sunil Singh
11-Jul-2017Try this
hope it helps you