[英]htmlAttributes: in Html.LabelFor
I created scaffolded view for Northwinds DB's product
table. 我为Northwinds DB's product
表创建了支架视图。 I understood that it is creating anonymous type at new {@class...
. 我知道它正在new {@class...
创建匿名类型。 But, I didn't understand portion htmlAttributes:
in the following code. 但是,在以下代码中,我不理解htmlAttributes:
部分。 What is it doing? 到底在做什么
@Html.LabelFor(model => model.UnitsInStock,
htmlAttributes: new { @class = "control-label col-md-2" })
And how is it different from new { htmlAttributes = new { @class = "form-control" }
this code? 与new { htmlAttributes = new { @class = "form-control" }
此代码有何不同? I hope I asked question properly. 我希望我能正确地提出问题。 I'm using MVC 5 with Visual Studio 2015. 我正在将MVC 5与Visual Studio 2015一起使用。
htmlAttributes:
is specifying a named parameter, so it is passing the anonymous object ( new { @class = "control-label col-md-2"
) to the htmlAttributes
parameter of the LabelFor()
method. htmlAttributes:
指定了一个命名参数,因此它将匿名对象( new { @class = "control-label col-md-2"
)传递给LabelFor()
方法的htmlAttributes
参数。
In this case its not strictly necessary because the LabelFor()
has an overload which accepts just the expression and the object
so it could also have been just 在这种情况下,它不是严格必需的,因为LabelFor()
具有仅接受表达式和object
的重载 ,因此它也可能只是
Html.LabelFor(m => m.UnitsInStock, new { @class = "control-label col-md-2" })
but using named parameter allows you to specify the parameters of a method in any order. 但是使用命名参数可以让您以任何顺序指定方法的参数。
Refer also the documentation for Named and Optional Arguments 另请参阅文档中的命名和可选参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.