Spiderman5
Spiderman5

Reputation: 23

Use ToString() for lambda in View

The field DateOfBirth has the type DateTime. I want to apply method ToString() in View:

@Html.DisplayFor(modelItem => Model.DateOfBirth).ToString("D")

But it doesn't work, because method ToString has 0 parameters.

I resolved this problem. In model I have the second field:

    public string DateOfBirth2
    {
        get { return DateOfBirth.ToString("D"); }
    }

And then I use this field in View.

But I want to know, if I can use method ToString("D") in View.

Upvotes: 0

Views: 387

Answers (1)

SLaks
SLaks

Reputation: 887195

You should display the property directly, without using DisplayFor:

@Model.DateOfBirth.ToString("D");

Upvotes: 1

Related Questions