Reyneer Leon
Reyneer Leon

Reputation: 456

What's the difference between (new { htmlAtributes = new { }) and (new { })

I know they are anonymous types but I don't get that Razor syntax. In some documentation I found an example like this:

@Html.Label("Hello", new { htmlAtributes = new { id = "hi" }})

In most cases I just use this simple: @Html.Label("Hello", new { id = "hi" })

Help me to understand this, and I don't know why my document.getElementById('hi').innerHTML = "changed"; doesn't work with the first one.

Upvotes: 3

Views: 62

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18973

The first way use for Html.LabelFor

@Html.LabelFor(c=>c.Email, new { htmlAtributes = new { id = "hi" } })

If you use @Html.Label("Hello", new { htmlAtributes = new { id = "hi" }})

it will generate to (with htmlatributes is an attribute of label tag)

<label for="Hello" htmlatributes="{ id = hi }">Hello</label>

You need use second way

@Html.Label("Hello", new { id = "hi" })

Upvotes: 2

Related Questions