C# htmlAttributes:在Html.LabelFor中
我为Northwinds DB的产品创建了脚手架视图。我知道它正在C# htmlAttributes:在Html.LabelFor中,c#,asp.net-mvc,visual-studio,C#,Asp.net Mvc,Visual Studio,我为Northwinds DB的产品创建了脚手架视图。我知道它正在新建{@class…中创建匿名类型。但是,我不明白下面代码中htmlAttributes:部分。它在做什么 @Html.LabelFor(model => model.UnitsInStock, htmlAttributes: new { @class = "control-label col-md-2" }) 它与new{htmlAttributes=new{@class=“form control”}这段代码
新建{@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()
有一个只接受表达式和对象的
Html.LabelFor(m => m.UnitsInStock, new { @class = "control-label col-md-2" })
但是使用命名参数允许您以任意顺序指定方法的参数
另请参阅文档,了解它只是将new{@class=“control label col-md-2”}
对象传递给扩展方法的htmlAttributes
参数。您还可以使用@Html.LabelFor(model=>model.UnitsInStock,new{@class=“control label col-md-2”})
(也就是说,没有明确使用htmlAttributes:
)它是c#中的命名参数特性吗?…为了澄清进一步的html属性是使用命名参数htmlAttributes:
传递的。这是(一个伟大的例子)C#中的功能。更多信息@StephenMuecke我想OP现在已经清楚了。你可以在回答中转换上面的评论,OP可以关闭帖子。