formatting DateTime error “Templates can be used only with field access, property access, single-dimension array index..”

前端 未结 5 1186
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 11:25

In MVC Razor view, I am trying to format a DateTime field to display time only. Using below code I am getting error \"Templates can be used only with field access, property acce

5条回答
  •  被撕碎了的回忆
    2021-02-20 11:53

    You have a very simple way to solve this and you might run into this problem more than once.

    To understand what's happening, you are passing a method as a parameter. Instead, you should be passing an expression.

    Instead of doing this

    @(Html.DisplayFor(m=>row.LastUpdatedDate.ToString("HH:mm:ss")))
    

    You should do

    var date = row.LastUpdatedDate.ToString("HH:mm:ss")
    @Html.DisplayFor(m=> date)
    

提交回复
热议问题