asp.net-mvc 如何从 Html.Label 的模型中获取价值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21989927/
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
How to get value from model for Html.Label?
提问by Rahmani
I want to place the value of a field (property) from model into Html.Label. Something like this:
我想将模型中的字段(属性)的值放入 Html.Label 中。像这样的东西:
@Html.Label(item => item.Title)
I don't want a label for item.Title (like Html.LabelFor( model => model.Title)). But I want to put the value of item.Titlein the label as text (string). So the result in run-time should be like this:
我不想要 item.Title 的标签(如Html.LabelFor( model => model.Title))。但我想将item.Title标签中的值作为文本(字符串)。所以运行时的结果应该是这样的:
<label>Some Title</label>
How can I do this?
我怎样才能做到这一点?
回答by melvas
Try this:
尝试这个:
@Html.Label(Model.Title)
It should work
它应该工作
EDITED
已编辑
or this:
或这个:
<label>@Html.DisplayFor(item => item.Title)</label>
回答by André Figueiredo
<label>@Model.Title</label>
回答by André Hauptfleisch
The following seems like a more appropriate way of adding a label to a field.
以下似乎是向字段添加标签的更合适的方法。
@Html.LabelFor(x => x.SomeValue, Model.SomeLabel)
@Html.TextBoxFor(x => x.SomeValue)
回答by H35am
I know this is an old question, but this helped me and maybe it will help someone else too.
我知道这是一个老问题,但这对我有帮助,也许对其他人也有帮助。
@Html.Label("Name of the label", htmlAttributes: new { @class = "control-label col-md-2" })

