TextBoxFor @Value (uppercase) instead @value

前端 未结 3 1058
南旧
南旧 2021-02-15 17:49

This is just for curiosity

Why does this code work:

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

and this doe

3条回答
  •  清歌不尽
    2021-02-15 18:22

    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 .

提交回复
热议问题