formatting DateTime error “Templates can be used o

2019-04-05 14:04发布

问题:

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 access, single-dimension array index, or single-parameter custom indexer expressions."

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

Any help please what is causing this error and how to fix it ?

Thanks for your help.

回答1:

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")


回答2:

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()) })


回答3:

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



回答4:

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>


回答5:

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>