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

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

    <div class="input-group date datetime col-md-8 col-xs-7" data-min-view="2" data-date-format="M dd yyyy">
     @Html.TextBox("closeDate", Model.closeDate.ToString("MMM dd yyyy"), new { @class = "form-control", @readonly = "readonly", size = "16", placeholder = Lang.Label_closeDate })
     <span class="input-group-addon btn btn-primary">
      <span class="glyphicon glyphicon-th"></span>
     </span>
    </div>
    
    0 讨论(0)
  • 2021-02-20 11:51

    I was having the same problem and I have resolved the problem. If you want to convert "LastUpdatedDate" to a specific format then you can try this:

    @Html.TextBoxFor(m=>row.LastUpdatedDate, 
        new { @Value = Convert.ToString(row.LastUpdatedDate.ToShortDateString()) })
    
    0 讨论(0)
  • 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

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

    You should do

    var date = row.LastUpdatedDate.ToString("HH:mm:ss")
    <td>@Html.DisplayFor(m=> date)</td>
    
    0 讨论(0)
  • 2021-02-20 11:55

    Another possible approach could be using extension methods to achieve the same. Using that approach you will not be populating your views with hard coded format.

    Extension Method

        /// <summary>
        /// Converts a DateTime string to Date string. Also if Date is default value i.e. 01/01/0001
        /// then returns String.Empty.
        /// </summary>
        /// <param name="inputDate">Input DateTime property </param>
        /// <param name="showOnlyDate">Pass true to show only date.
        /// False will keep the date as it is.</param>
        /// <returns></returns>
        public static string ToString(this DateTime inputDate, bool showOnlyDate)
        {
            var resultDate = inputDate.ToString();
    
            if (showOnlyDate)
            {
                if (inputDate == DateTime.MinValue)
                {
                    resultDate = string.Empty;
                }
                else
                {
                    resultDate = inputDate.ToString("dd-MMM-yyyy");
                }
            }
            return resultDate;
        }
    

    View

     @Model.LastUpdateDate.ToString(true).
    

    Link: DateTime to Date in ASP.net MVC

    0 讨论(0)
  • 2021-02-20 11:57

    DisplayFor expects an expression that identifies the object that contains the properties to display. It will use built in, or custom, templates to render that display. You trying to provide the display logic as that expression parameter, which is not valid.

    Use

    @String.Format("HH:mm:ss", Model.row.LastUpdateDate)
    

    or

    @Model.row.LastUpdateDate.ToString("HH:mm:ss")
    
    0 讨论(0)
提交回复
热议问题