x): @H" />

局部的和DisplayFor局部、DisplayFor

由网友(爱吃喵的鱼)分享简介:首先我的模型: public class Order{public DateTime Date {get;set;}}我的观点: @model Order@Html.Partial("Display", Model.Date)显示部分: @Html.LabelFor(x => x): @H...

首先我的模型:

public class Order
{
  public DateTime Date {get;set;}
}

我的观点:

@model Order
@Html.Partial("Display", Model.Date)

显示部分:

@Html.LabelFor(x => x): <strong>@Html.DisplayFor(x => x)</strong>

标签不渲染。该DisplayFor呈现日期罚款。可能也是相关的,如果我添加一个属性Order.Date [DisplayName的(订购日期)] 它没有兑现。有什么我做错了什么?我只是想一个HTML约定适用的所有的我的模型显示属性。我要对这个错误?

The label is not rendering. The DisplayFor renders the date fine. Also probably related, if I add an attribute to Order.Date [DisplayName("Order Date")] it is not honored. There something I am doing wrong? I simply want to apply a html convention to all my model properties for display. Am I going about this wrong?

推荐答案

这是因为你的部分基本上是强类型的的DateTime ,不是订单。这仍然适用于 DisplayFor ,因为的DateTime 将只是调用的ToString()本身。但是,它没有名字的或固有的标签,所以 LabelFor 打算做什么你。

This is because your partial is essentially strongly-typed for DateTime, not Order. This still works for DisplayFor because a DateTime will just call ToString() on itself. But, it has no name or inherent label, so LabelFor is going to do nothing for you.

你应该这样做的方式,实际上是通过了订单实例到部分为型号。然后:

The way you should be doing this, is actually passing the Order instance into the partial as the Model. Then:

@Html.LabelFor(m => m.Date): <strong>@Html.DisplayFor(m => m.Date)</strong>

会得到你,你找什么。

Will get you what you're looking for.

阅读全文

相关推荐

最新文章