@html.labelfor 多语言,Html.LabelFor Specified Text

Anyone got any idea on how to specify text when using Html.LabelFor(c=>c.MyField). It's just MyField might not be an appropriate name to display on screen, you may want "The Super Fantastic Field" instead, but there doesn't appear to be any overloads.

Any ideas?

[Display(Name = "My Field")]

public string MyField { get; set; }

Setting the ResourceType property on your attribute will allow you to use a resource file.

(Prior to .NET 4 use System.ComponentModel.DisplayNameAttribute with the caveat that the display name must be a compile-time constant.)

Easy solution just add the following in the view:

@Html.LabelFor(c=>c.MyField, "My Field")

There is a new overload in MVC 3 so you should be able to specifiy custom test for the labelfor helper.

I haven't downloaded v2 yet, so I can't test, but I believe it works like DynamicData, in which case you'd do something like this on your model:

[Display(Name = "The Super Fantastic Field")]

public string MyField {get;set;}

There are two ways

1"direct annotations"

2"Annotatinos with a resource"

Direct annotations

[Display(Name = "My Field")]

public string MyField { get; set; }

Annotatinos with a resource

[Display(Name = "My_Field",ResourceType = typeof(Resource))]

public string MyField { get; set; }

Second way will require to add a value in resource file probably named as Resource.resx.

Use which suits your purpose.

I haven't checked out CP1 yet but I read over Scott's release of it and I seem to recall that the code was generated by T4. I suppose you could always mod that, but I would suspect that they will provide overloads in CP2.

Edit: The source is always available and thus you could just mod the method, change the T4 generator, and you'll be good to go. Also put in a ticket or request (somehow) for that mod so it gets worked into the next version.

There are 5 overloads. Several offer second parameter of "string labelText", which you would set to "The Super Fantastic Field".

来源:https://stackoverflow.com/questions/1312880/html-labelfor-specified-text

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值