局部的和DisplayFor [英] Partial And DisplayFor

查看:218
本文介绍了局部的和DisplayFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我的模型:

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.

这篇关于局部的和DisplayFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆