gpt4 book ai didi

c# - 用于 DisplayFor 的 IEnumerable 类型 View 模型的 Razor 语法

转载 作者:行者123 更新时间:2023-11-30 21:03:37 42 4
gpt4 key购买 nike

我正在从 SQL 数据库中以 hh:mm dd/mm/yyyy 格式提取日期时间

使用 Razor 语法,我可以显示存储在数据库中的日期

@Html.DisplayFor(modelItem => item.Updated)

我希望 Razor 以以下格式显示 dd MMM yyyy 所以我根据另一个 StackOverflow 答案尝试了以下操作:

@Html.DisplayFor(modelItem => item.Updated.ToString("dd MMM yyyy"))

Converting DateTime format using razor

但是这会产生以下错误:

Server Error in '/' Application.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Source Error:

Line 36: Line 37: Line 38:
@Html.DisplayFor(modelItem => item.Updated.ToString("dd MMM yyyy")) Line 39: Line 40:

我刚刚开始掌握 ASP.NET 等......我不确定如何创建 View 模型来尝试这个示例,因为我首先使用自动生成的 edmx 文件构建数据库:

[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")] 
pubilc DateTime Updated { get; set }

有什么建议吗?

目前这些工作。我想要做的就是更改“已更新”属性的显示格式。我的 View 文件: http://pastebin.com/Ywn8Z0TV

我的 Controller : http://pastebin.com/1jAih6AE

我的模型: http://pastebin.com/UC61QjFZ

更新:对于那些声明应该是 item => item.Updated...

Error   1   A local variable named 'item' cannot be declared in this scope because it would give a different meaning to 'item', which is already used in a 'parent or current' scope to denote something else

最佳答案

您的解决方案不起作用的具体原因与错误状态完全相同。

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

所以 ToString()是一种方法,不适用于模板。

其他人已经给出了替代方案,我想解释一下为什么你的方案不起作用。

I am just getting to grips with ASP.NET etc... and I am not certain how to create a view model to try this example because I am building DB first with an auto generated edmx file

您的 edmx 文件应该创建一个看起来也类似的类:

public partial class SomeTable
{
public DateTime Updated { get; set }
}

在同一个项目中,在同一个命名空间(不一定是目录)中创建一个名为SomeTable.Partial.cs 的文件看起来也很相似:

public partial class SomeTable
{
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
pubilc DateTime Updated { get; set }
}

Entity Framework 默认创建Partial Classes代表您选择的表格。这意味着您可以在另一个文件中定义另一个分部类,并且 .Net 会在编译时将每个属性的所有属性组合到一个属性中。

更新

我觉得这段代码很奇怪,我不明白它是如何工作的:

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Metal.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Currency.Code)

更新 2

DisplayFor用于显示模型的属性(并且仅直接显示模型的属性)。当您枚举 IEnumerable<> 类型的模型时(或模型上的属性),您不能再使用 DisplayFor显示这些值。你应该使用 Display 反而。例如,Metal.NameIEnumerable 上不存在, 它存在于 IEnumerable 中的一个元素中.

@foreach (var item in Model) {
<tr>
<td>
@Html.Display(item.Metal.Name)
</td>
<td>
@Html.Display(item.Currency.Code)

错误更新

Error 1 A local variable named 'item' cannot be declared in this scope because it would give a different meaning to 'item', which is already used in a 'parent or current' scope to denote something else

当存在如下代码时:

@foreach (var item in Model) // or Model.Items
{
<div>
@Html.DisplayFor(item => item.Updated)
</div>
}

所有 MVC EditForDisplayFor方法使用 Expression<TDelegate>所以当代码声明 DisplayFor(item => item.Updated)第一个item基本上是定义一个变量 T item但是您不能再次定义相同的变量名。为了说明这不是 MVC 特定的,这也不会编译并给出相同的错误(并且对于相同变量名的声明更明显):

var item = 1;
var items = new List<int>() { 1, 2, 3, 4};
var sameItem = items.Select(item => item == item);

关于c# - 用于 DisplayFor 的 IEnumerable<T> 类型 View 模型的 Razor 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731315/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com