- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 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.Name
在 IEnumerable
上不存在, 它存在于 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 EditFor
和 DisplayFor
方法使用 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/
View 模型: public class LiquiditySummariesViewModel { [DisplayFormat(ApplyFormatInEditMode = true,
所以下拉列表有值/文本。该值是我存储在数据库中的值(因为它是列出要在下拉列表中显示的值/文本的表的主键)。当我填充我的下拉列表时,我使用值/文本。当用户选择时,它将值(数字)存储到事务表中。这意味着当
我遇到了无法调用正确的 DisplayTemplate 的问题。 我有两个模型: CompanyName.Application.Web.Controllers.SharedViewModels.Pa
所以我有一个 MVC 应用程序来管理站点列表,并且在我的详细信息 View 中,即使 @Html.DisplayNameFor 正确呈现标签,我的模型中的子类也没有呈现任何内容。 这是工作正常的 Vi
我在 MVC 5 应用程序中遇到自定义显示模板问题。我有一个 View 模型,其中包含一个我想重用的复杂类型。我创建了 DisplayTemplates 文件夹并放置了一个遵循正确命名约定的局部 Vi
我对 Razor 很陌生,所以可能会遗漏一些明显的东西。我有一个: @Html.DisplayFor(model => model.ColourValue, "") 从模型返回颜色值。例如:#0040
我有一个模型: public class PostModel { public int PostId { get; set; } [Required] [Display(Nam
我有一个 MVC4 razor View ,我将在其中使用元数据的反射来显示数据表,以在运行时动态构建列。 @Html.DisplayFor 不会出现 int 列,但会出现 string、bool 等
我想在每个 language.Language.NativeName 后面加一个逗号现在他们都在一条直线上出来。我不能只在 NativeName get 方法中添加一个逗号,因为我也会将它用于其他内容
我有两个要显示的值:“描述”用括号括起来 @Html.DisplayFor(modelItem => item.Name) @Html.DisplayFor(modelItem =
我有以下看法:- @foreach(var info in Model.Firewall.FirewallCustomers.OrderBy(a=>a.CustomerName)){ @Html.Di
我喜欢 MVC 2。整个东西非常适合网络。 但是,有一项功能我无法从 Html.DisplayFor() 函数中提取出来: >"> 我需要能够为此值使用 DisplayTemplate。
我需要在 DisplayFor HtmlHelper 中显示相应值的枚举名称。我有以下枚举: public enum CheckStatus { Yes = 1,
MVC6 引入了标签帮助程序,与使用 @Html.EditorFor 等相比,这是一种更好的方法。但是我还没有找到任何标签帮助程序可以替代 @Html.DisplayFor。 当然,我可以直接在 Ra
我有一个使用 ASP.NET MVC 5 制作的 Web 应用程序。在这个应用程序中,我有一个显示信息列表的 View 。 在此列表中,我使用 @Html.DisplayFor(modelItem =
我有一个使用 ASP.NET MVC 5 制作的 Web 应用程序。在这个应用程序中,我有一个显示信息列表的 View 。 在此列表中,我使用 @Html.DisplayFor(modelItem =
我创建了一个 htmlhelper 扩展来减少创建表单时的重复标记量: public static MvcHtmlString RenderField(this HtmlHelper htmlHelp
如果我有一个对象并查看为: public MyCustomerObject { public int Id {get;set;} public string Label {get;se
我创建了一个 ViewModel,我将其用作 View 的模型。在 View 模型中,它包含以下定义: public class ItemDetailViewModel { public in
@foreach (var item in Model) { @Html.DisplayFor(modelItem => item.nume, "Name")
我是一名优秀的程序员,十分优秀!