htmlAttributes: in Html.LabelFor
本文关键字:LabelFor Html in htmlAttributes | 更新日期: 2023-09-27 18:19:48
我为Northwinds DB's product
表创建了脚手架视图。我知道它正在new {@class...
创建匿名类型。但是,我不理解下面代码中的htmlAttributes:
部分。它在做什么?
@Html.LabelFor(model => model.UnitsInStock,
htmlAttributes: new { @class = "control-label col-md-2" })
它和new { htmlAttributes = new { @class = "form-control" }
这个代码有什么不同?我希望我问得对。我在Visual Studio 2015中使用MVC 5。
htmlAttributes:
正在指定一个命名参数,因此它将匿名对象(new { @class = "control-label col-md-2"
)传递给LabelFor()
方法的htmlAttributes
参数。
在这种情况下,这不是严格必要的,因为LabelFor()
有一个过载,它只接受表达式和object
,所以它也可能只是
Html.LabelFor(m => m.UnitsInStock, new { @class = "control-label col-md-2" })
但是使用命名参数可以按任何顺序指定方法的参数。
另请参阅命名和可选参数的文档