Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# htmlAttributes:在Html.LabelFor中_C#_Asp.net Mvc_Visual Studio - Fatal编程技术网

C# htmlAttributes:在Html.LabelFor中

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”}这段代码

我为Northwinds DB的产品创建了脚手架视图。我知道它正在
新建{@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可以关闭帖子。