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

前端 未结 5 1181
被撕碎了的回忆
被撕碎了的回忆 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:46

    Instead of TextBoxFor use TextBox and specify the format in the value field i.e.

    @Html.TextBox("textboxName", Model.dateName.ToString("MMM dd yyyy"))
    

    Using bootstrap datepicker this would look like:

    @Html.TextBox("closeDate", Model.closeDate.ToString("MMM dd yyyy"), new { @class = "form-control", @readonly = "readonly", size = "16", placeholder = Lang.Label_closeDate })

提交回复
热议问题