Inserting non-breaking space in @ Html.DisplayFor when data is empty or empty? ASP.Net MVC

I need to insert  through @HTML.DisplayForwhen the support model value is null or empty.

I tried using data annotations

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

which really works to stick to the "" where I expect this, but instead I need to put a non-expanding space.

+4
source share
3 answers

This works for me:

NullDisplayText = @" ", HtmlEncode = false
+1
source

Try something like:

var space = " ";

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

" " , #. , HTML- , .

+1

, @Html.DisplayFor. .

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

.

@FooHelper.NonBreakingSpacesIfNullOrEmpty(Model.MiddleName, 1)

spaces , .

0

Source: https://habr.com/ru/post/1527568/


All Articles