You can write a user assistant that will conditionally display the contents:
public static class HtmlExtensions { public static IHtmlString FormatValue( this HtmlHelper htmlHelper, string value, string label ) { if (string.IsNullOrWhiteSpace(value)) { return MvcHtmlString.Empty; } var result = string.Format( "<p><strong>{0}</strong> {1}</p>", htmlHelper.Encode(label), htmlHelper.Encode(value) ); return new HtmlString(value); } }
and then:
@Html.FormatValue( Model.Atendimento.PrazosEEntregas.ObservacaoPrazoRetirada, "Observação:" )
Another possibility is to use a display template:
@Html.DisplayFor(x => x.Atendimento.PrazosEEntregas.ObservacaoPrazoRetirada)
Then you can define a display pattern for the string type (or custom):
@model string @if (!string.IsNullOrWhiteSpace(Model)) { <p> <strong>@ViewData.ModelMetadata.DisplayName</strong> @ViewData.TemplateInfo.FormattedModelValue </p> }
and on your view model:
[DisplayName("Observação:")] [UIHint("MyTemplate")] public string ObservacaoPrazoRetirada { get; set; }
source share