c# - How Do I Get WebGrid To Display Email Link Only If There's Data In The item.Email Field? -


i have webgrid , i'm trying pretty bit showing mailto link if there's content in contactemail field of sql query. if there's nothing in contactemail field (i.e. null or "") want display nothing @ all.

the current version has bog standard:

locationsgrid.column("contactemail", "email") 

but displays whatever in field text. not aesthetically pleasing.

if go for:

locationsgrid.column("contactemail", "email",  format: @<text><a href="mailto:@item.contactemail " target="_top">email</a></text>) 

i text "email" in every row, having text in email field getting link (as want) , without getting in plain text (which don't want).

i've tried:

locationsgrid.column("contactemail", "email",  format: (item.contactemail == null || item.contactemail == "" ? "" : @<text><a href="mailto:@item.contactemail " target="_top">email</a></text>)) 

but i'm getting:

compiler error message: cs0103: name 'item' not exist in current context 

could me rectify this?

have tried explicitly using item in lamdba function within format section :

format: (item) => (string.isnullorempty(item.contactemail) ? "" : @<text><a href="mailto:@item.contactemail" target="_top">email</a></text>) 

or if prefer more verbose approach :

grid.column(format: (item) => {      if (!string.isnullorempty(item.contactemail))      {          return html.raw("<a href='mailto:@item.contactemail' target='_top'>email</a>"));      } }) 

Comments