下面的代码是我的cshtlm页面:
@using (Html.BeginForm("Optional", "ConfigurationSilo2", FormMethod.Post, new { id = "storeDataForm" }))
{
@Html.TextBoxFor(x => x.Height)
<div align="right">
<input type="submit" id="Avanti" value="@ViewRes.ConfigurationString.buttonNext" name="@ViewRes.ConfigurationString.buttonNext" />
</div>
}我生成的html代码是:
<form action="/ConfigurationSilo2/Optional" id="storeDataForm" method="post">
<input data-val="true" data-val-number="The field Height must be a number." data-val-required="The Height field is required." id="Height" name="Height" type="text" value="0" />
<div align="right">
<input type="submit" id="Avanti" value="Next" name="Next" />
</div>
</form>这是我的模型代码类:
public class Spondina
{
public int Height { get; set; }
public int Quantity { get; set; }
public float UnitPrice { get; set; }
public float TotalCost { get; set; }
}为什么我的输入标签中有标签data-val data-val-number data-val required?
发布于 2013-04-15 10:48:42
这就是MVC验证的开始。您的模型上有数据注释吗?我猜Height属性对它有[Required]吗?这些属性由非突出的客户端验证框架使用。下面是输入助手中添加这些标记的行:
tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));HtmlHelper有一个ClientValidationRuleFactory,它在HtmlHelper的构造函数中初始化,其中包括一个ClientDataTypeModelValidatorProvider,它检查模型的元数据,并在模型中的数字类型的情况下应用适当的验证规则。如果您在模型中启用了客户端验证和数字类型,例如Height作为int,则在呈现期间,帮助程序将不引人注目的客户端验证注入到这些输入中。
https://stackoverflow.com/questions/16013171
复制相似问题