Properties With DisplayFormat Attribute And Timestamp Not Showing Correctly In Blazor Using DisplayFor(). DisplayFor() Isn't Recognized In Blazor.
Hi All,
Cheers!
Given that we were migrating a proof of concept from ASP.NET Core MVC to Blazor, we were used to using the DisplayFor() method to show
the values of the properties decorated with DataAnnotations attributes such as the example below.
[DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Start Date")] public DateTime StartDate { get; set; } = DateTime.Now; [DataType(DataType.Currency)] [Column(TypeName = "money")] public decimal Budget { get; set; }
However DisplayFor() isn't recognized method in Blazor, so what we did was to create a display property to return the values with proper format strings. Note that we added a NotMapped attribute so that when we perform database migrations, these properties are ignored.
[NotMapped] public string DisplayDate { get { return EnrollmentDate.ToString("yyyy-MM-dd"); } } [NotMapped] public string DisplayBudget { get { return Budget.ToString("C"); } }
Cheers!
Comments
Post a Comment