Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a nice way to control how display templates should render null fields?

Is there a way to control how built-in display templates (like decimal, string) should render null fields / empty string? I'd want to show "-" as field value for some and empty line for others.

I know I can check those fields for null in the views, but this seems like a not nice thing to do in hundreds of places.

like image 835
mrówa Avatar asked Oct 03 '13 16:10

mrówa


1 Answers

Apply the NullDisplayText property of the DisplayFormat attribute to the relevant view model members.

[DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "-")]   
public string Name { get; set; }

Note, the DisplayFormat attribute is intended to be used with templated helpers such as EditorFor and DisplayFor (which it sounds like you are doing).

like image 171
Pero P. Avatar answered Oct 09 '22 08:10

Pero P.