C# Html.LabelFor 指定文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1312880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:59:43  来源:igfitidea点击:

Html.LabelFor Specified Text

c#asp.net-mvcrazor

提问by Kezzer

Anyone got any idea on how to specify text when using Html.LabelFor(c=>c.MyField). It's just MyFieldmight 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.

任何人都知道如何在使用Html.LabelFor(c=>c.MyField). 它MyField可能不是在屏幕上显示的合适名称,您可能想要“The Super Fantastic Field”,但似乎没有任何重载。

Any ideas?

有任何想法吗?

采纳答案by Curtis Buys

You use System.ComponentModel.DataAnnotations.DisplayAttribute:

你使用System.ComponentModel.DataAnnotations.DisplayAttribute

[Display(Name = "My Field")]
public string MyField { get; set; }

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

ResourceType在您的属性上设置属性将允许您使用资源文件。

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

(在 .NET 4 使用之前,System.ComponentModel.DisplayNameAttribute需要注意显示名称必须是编译时常量。)

回答by Chance

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.

我还没有检查过 CP1,但我阅读了 Scott 的版本,我似乎记得代码是由 T4 生成的。我想你总是可以修改它,但我怀疑他们会在 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.

编辑:源始终可用,因此您只需修改方法,更改 T4 生成器,就可以了。还为该 mod 放入一张票或请求(以某种方式),以便它可以用于下一个版本。

回答by Daniel

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:

我还没有下载 v2,所以我不能测试,但我相信它像 DynamicData 一样工作,在这种情况下,你可以在你的模型上做这样的事情:

[Display(Name = "The Super Fantastic Field")]
public string MyField {get;set;}

回答by Joe Cartano

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

MVC 3 中有一个新的重载,因此您应该能够为 labelfor helper 指定自定义测试。

回答by Faisal Khalid

Easy solution just add the following in the view:

简单的解决方案只需在视图中添加以下内容:

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

回答by yogibeare

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

有5个重载。有几个提供“string labelText”的第二个参数,您可以将其设置为“The Super Fantastic Field”。

回答by Baimyrza Shamyr

There are two ways
1"direct annotations"
2"Annotatinos with a resource"
Direct annotations

有两种方式
1“直接注释”
2“带有资源的
注释”直接注释

[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.

第二种方法需要在可能命名为Resource.resx 的资源文件中添加一个值 。
使用适合您的目的。