gmail user
gmail user

Reputation: 2783

htmlAttributes: in Html.LabelFor

I created scaffolded view for Northwinds DB's product table. I understood that it is creating anonymous type at new {@class.... But, I didn't understand portion htmlAttributes: in the following code. 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? I hope I asked question properly. I'm using MVC 5 with Visual Studio 2015.

Upvotes: 1

Views: 5433

Answers (1)

user3559349
user3559349

Reputation:

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.

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

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

Upvotes: 3

Related Questions