6

I have a need to insert a   via @HTML.DisplayFor when the backing model's value is null or empty.

I've tried using data annotations

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

which does work to stick a " " where I expect it but I need to put a non-breaking space there instead.

John S
  • 7,909
  • 21
  • 77
  • 145

3 Answers3

2

This works for me:

NullDisplayText = @" ", HtmlEncode = false
  • 2
    A word of caution on this approach. If the model's value is not null and contains a character that should be encoded, such as a less than sign, then the output may not render correctly. In addition, this approach is vulnerable to HTML script injection if the model's value is from an untrusted source. – Mike Grove aka Theophilus Sep 14 '15 at 13:16
1

Try something like:

var space = " ";

 [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText ="@space" ]
public string  MiddleName { get; set; }

The issue with placing " " in that location is that it will be read as c# code. Instead, you want it to be read as HTML code at run time, and the above should achieve this.

James
  • 216
  • 1
  • 7
  • Tried a number of variations of your code above with no luck. I assume that you are trying to get the @space text to be placed in the HTML stream and then interpreted as Razor code? – John S Feb 18 '14 at 20:16
  • Have you looked at this answer:(http://stackoverflow.com/questions/524528/asp-net-mvc-html-encode-new-lines) – James Feb 18 '14 at 21:58
  • Have you tried '[DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = Environment.NewLine ] public string MiddleName { get; set; }' ? – James Feb 18 '14 at 22:07
  • James, that throws an exception "attribute argument must be a constant expression of an attribute parameter type." – John S Feb 18 '14 at 22:14
  • Forgot it is not a constant and only used at runtime. With that said, try using 'var space = Environment.NewLine;' '...NullDisplayText = "@space"' If that doesn't work, is it populating an error? – James Feb 18 '14 at 22:35
  • This just displays @space when there is no value. – John S Feb 20 '14 at 05:53
  • Ok, let me make test scenario and see if I can figure this out. – James Feb 20 '14 at 06:02
  • Ok, so after a bit of research and a looking into a few things, give this a try `[DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText ="<%=&nbsp%>" ] public string MiddleName { get; set; }`. Let me know. – James Feb 20 '14 at 22:38
  • Sorry that just gives me a <%=&nbsp%> displayed on the page. – John S Feb 22 '14 at 01:53
0

I worked around this problem by writing a helper instead of using @Html.DisplayFor. This is it.

@helper NonBreakingSpacesIfNullOrEmpty(string field, int spaces)
{
    if (String.IsNullOrEmpty(field))
    {
        @(new HtmlString(String.Concat(Enumerable.Repeat("&nbsp;", spaces))))
    }
    else
    {
        @field
    }
}

Your code would call it like this.

@FooHelper.NonBreakingSpacesIfNullOrEmpty(Model.MiddleName, 1)

Delete the spaces parameter and the associated logic if you don't need the repeat functionality.