Paramone Paramone - 4 months ago 7
HTML Question

@Html.DisplayFor — Adding extra plain text

I have two values that I want to

@Html.DisplayFor(modelItem => item.Name)
@Html.DisplayFor(modelItem => item.Description)


But instead of writing it like:

@Html.DisplayFor(modelItem => item.Name)
(
@Html.DisplayFor(modelItem => item.Description)
)


I'd like to clean it up and write it like:

@Html.DisplayFor(modelItem => item.Name + "(" + item.Description + "("))


Similar to PHP Echo

Is this possible? If so, how?

Answer

According to your comment, you want to display the Description surrounded by brackets only when Description is not empty, so add a new property in your model as follows

public string NameAndDescription
{
    get
    {
        if (string.IsNullOrEmpty(this.Description))
        {
            return this.Name;
        }
        else
        {
            return this.Name + " (" + this.Description + ")";
        }
    }
}

then display the new property in your view like this

@Html.DisplayFor(modelItem => item.NameAndDescription)
Comments