C# 格式化日期时间错误“模板只能用于字段访问、属性访问、单维数组索引..”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19417311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:56:17  来源:igfitidea点击:

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

c#asp.net-mvcasp.net-mvc-3razor

提问by Toubi

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

在 MVC Razor 视图中,我试图格式化 DateTime 字段以仅显示时间。使用下面的代码我收到错误“模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式。”

<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.

谢谢你的帮助。

采纳答案by Mufaka

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.

DisplayFor 需要一个表达式来标识包含要显示的属性的对象。它将使用内置或自定义模板来呈现该显示。您试图提供显示逻辑作为该表达式参数,这是无效的。

Use

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

or

或者

@Model.row.LastUpdateDate.ToString("HH:mm:ss")

回答by user3517975

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:

我遇到了同样的问题,我已经解决了这个问题。如果要将“LastUpdatedDate”转换为特定格式,则可以尝试以下操作:

@Html.TextBoxFor(m=>row.LastUpdatedDate, 
    new { @Value = Convert.ToString(row.LastUpdatedDate.ToShortDateString()) })

回答by Rohit

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

链接:ASP.net MVC 中的 DateTime to Date

回答by dunwan

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

代替 TextBox 使用 TextBox 并在值字段中指定格式,即

@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>

回答by Daniel

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>