Skip to content

Instantly share code, notes, and snippets.

@conficient
Created June 24, 2021 14:24
Show Gist options
  • Save conficient/eab57ade2587104d71ac6f26ddfd4865 to your computer and use it in GitHub Desktop.
Save conficient/eab57ade2587104d71ac6f26ddfd4865 to your computer and use it in GitHub Desktop.
NotNull.razor

NotNull

To avoid NullReferenceExceptions, lots of code in Razor/Blazor looks like this:

  @if(someVar != null) 
  {
       <p>use @someVar.Property</p>
  }

With NotNull you can use this:

  <NotNull Value="someVar">
    <p>use @someVar.Property</p>
  </NotNull>
@if (Value != null)
{
@ChildContent
}
@code
{
/* sample usage
* <NotNull Value="yourValue">
* // use value
* </NotNull>
*/
/// <summary>
/// Value to track - will only display child content if this value is not null
/// </summary>
[Parameter] public object Value { get; set; }
/// <summary>
/// Child content to display
/// </summary>
[Parameter] public RenderFragment ChildContent { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment