Html.Label,Html.LabelFor和Html.LabelForModel有什么区别 [英] What's the difference between Html.Label, Html.LabelFor and Html.LabelForModel

查看:147
本文介绍了Html.Label,Html.LabelFor和Html.LabelForModel有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Html.Label()@Html.LabelFor()@Html.LabelForModel()方法之间有什么区别?

What's the difference between @Html.Label(), @Html.LabelFor() and @Html.LabelForModel() methods?

推荐答案

Html.Label为您提供名称与指定输入文本匹配的输入标签(更具体地说,与字符串表达式匹配的model属性):

Html.Label gives you a label for an input whose name matches the specified input text (more specifically, for the model property matching the string expression):

// Model
public string Test { get; set; }

// View
@Html.Label("Test")

// Output
<label for="Test">Test</label>

Html.LabelFor为您提供的表达式表示的属性的标签(通常是模型属性):

Html.LabelFor gives you a label for the property represented by the provided expression (typically a model property):

// Model
public class MyModel
{
    [DisplayName("A property")]
    public string Test { get; set; }
}

// View
@model MyModel
@Html.LabelFor(m => m.Test)

// Output
<label for="Test">A property</label>

Html.LabelForModel有点棘手.它返回一个标签,其标签的for值是模型对象表示的参数的值.这对于自定义编辑器模板尤其有用.例如:

Html.LabelForModel is a bit trickier. It returns a label whose for value is that of the parameter represented by the model object. This is useful, in particular, for custom editor templates. For example:

// Model
public class MyModel
{
    [DisplayName("A property")]
    public string Test { get; set; }
}

// Main view
@Html.EditorFor(m => m.Test)

// Inside editor template
@Html.LabelForModel()

// Output
<label for="Test">A property</label>

这篇关于Html.Label,Html.LabelFor和Html.LabelForModel有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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