Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBoxFor @Value (uppercase) instead @value

This is just for curiosity

Why does this code work:

Html.TextBoxFor(x => x.Age, new { @Value = "0"})

and this doesn't:

Html.TextBoxFor(x => x.Age, new { @value = "0"})

Note the uppercase 'V' in @Value

I know value is a keyword, but so is readonly and it works. It's not necessary to use @Readonly (with uppercase 'R').

Does anybody have a clue?

like image 350
Diego Avatar asked Jan 03 '13 16:01

Diego


2 Answers

InputExtensions.TextBoxFor special cases cases a few attribute names, among them value(case sensitive). This is unrelated to C# keywords.

In particular the value obtained from the expression parameter takes precedence of a property called value you pass into the htmlAttributes parameter.

Taking a look at your example:

  • If you use Html.TextBoxFor(x => x.Age, new { @value = "0"}) it will compile, but TextBoxFor will override the value attribute with the value x.Age evaluates to.

  • If you use Html.TextBoxFor(x => x.Age, new { @Value = "0"}) it will compile, and you will get two entries in the attribute dictionary, one Value that's "0", and one value, that's x.Age.

    I expect the output to be something nonsensical like <input Value="0" value="..." type="text"/>.

like image 116
CodesInChaos Avatar answered Oct 01 '22 02:10

CodesInChaos


I'm not 100% sure but, that could be value is a keyword in properties, readonly isn't. Look at properties from MSDN.

enter image description here

enter image description here

like image 45
Soner Gönül Avatar answered Oct 01 '22 04:10

Soner Gönül