Paramone
Paramone

Reputation: 2724

@Html.DisplayFor — Adding extra plain text

I have two values that I want to show: with brackets around 'Description'

@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?

Upvotes: 4

Views: 755

Answers (1)

ekad
ekad

Reputation: 14614

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)

Upvotes: 3

Related Questions