DevDevDev
DevDevDev

Reputation: 649

Razor view disabled html attribute based on viewmodel property

I want to set an html disabled attribute based on a viewmodel property.

I wrote:

<button disabled="@Model.prop">Button</button>

This works, but I'm not sure why. When true, it sets disabled="disabled", I would expect it to set disabled="True" which would disable the element by setting the disabled attribute at all. When false, the disabled attribute is entirely gone, whereas I would expect disabled="False" and for this to also disable the element.

I don't understand how this is being evaluated on the server when it generates the view so I was hoping someone could help me understand.

Upvotes: 1

Views: 791

Answers (1)

Zach dev
Zach dev

Reputation: 1620

Html disabled attribute not need set a value, if you set disabled="true" or disabled="false" both ways gonna disabled just because have the disabled attribute and that's why razor when its false remove the attribute

see the reference

Example

<input type="text" disabled="true" value="true">
<input type="text" disabled="false" value="false">
<input type="text" disabled value="just attribute">
<input type="text" value="without attribute">

Upvotes: 1

Related Questions