Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check for null/empty value in cshtml

<b>Start Date: </b>@employee["StartDate"].<br />

Using MVC Razor 3/C#, how can I check if employee["StartDate"] value is null/empty in the cshtml? So that if it is, I instead display:

<b>Start Date: </b>Unknown.<br />

I tried:

@if(employee["StartDate"] == null){<b>Start Date: </b>Unknown.<br />} 

but that doesn't work.

like image 758
sharcfinz Avatar asked Jun 24 '13 18:06

sharcfinz


1 Answers

Try

<b>Start Date: </b>@(employee["StartDate"] ?? "Unknown").<br />

?? return the left-side value, or the right-side value if the left-side value is null.

like image 190
D Stanley Avatar answered Nov 15 '22 19:11

D Stanley