简体   繁体   English

如何使用Html.TextBoxFor(C#)设置默认值,使其符合W3C标记?

[英]How to set a default value with Html.TextBoxFor (C#) so it is W3C markup compliant?

I am setting a default value with the following code: 我使用以下代码设置默认值:

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

The problem is the generated code contains two value attributes, breaking W3C markup validation. 问题在于生成的代码包含两个值属性,破坏了W3C标记验证。

<input Value="0" data-val="true" data-val-required="Age is required." id="Age" name="Age" type="text" value="" />

Notice the capital 'V' and the lowercase 'v' value attributes... 注意大写的'V'和小写的'v'值属性...

Does anyone know of a work around for the html helper TextBoxFor to only have one value attribute?? 有谁知道html帮助器TextBoxFor仅具有一个value属性的解决方法?

Get rid of the "new { @Value = "0"}" and just use the following... 摆脱“新的{@Value =“ 0”}“”,只需使用以下内容即可...

@Html.TextBoxFor(m => m.Age)

The value of the input will be whatever value the Age property of the model is. 输入的值将是模型的Age属性的任何值。 If you want the default value to be 0, than set the age property of the model to 0. 如果希望默认值为0,则将模型的age属性设置为0。

This happen because the Value is not the the same attribute the value , HTML 5 is case sensitive and the Html.TextBoxFor give the output rightly (considering value is the right and Value a custom). 发生这种情况是因为Value不是value的同一个属性,HTML 5区分大小写,并且Html.TextBoxFor正确地提供了输出(考虑value是正确的,而Value是自定义的)。

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM