@ Html.DisplayFor - Adding Extra Plain Text

I have two values ​​that I want to show: using brackets around the "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 would like to clean it and write it like this:

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

Like PHP Echo

Is it possible? If so, how?

+4
source share
1 answer

According to your comment, you want to display Descriptionsurrounded by brackets only when it is Descriptionnot empty, so add a new property to 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 as shown below.

@Html.DisplayFor(modelItem => item.NameAndDescription)
+3
source

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


All Articles