无法验证ASP.NET MVC中整数输入的空值?
问题描述:
如何在ASP.NET MVC中使用DataAnnotations验证Score属性?无法验证ASP.NET MVC中整数输入的空值?
[Required(ErrorMessage = "Required field")]
public int Score { get; set; }
我可以用@ Html.TextBoxFor验证这一领域:
@Html.TextBoxFor(m => m.Score, new { name = "Score", id = "Score",
@class = "form-control" , @Value = "" })
但无法验证整数输入:
<input type="text" id="Score" name="Score" value="" >
<script>
$("input[name='Score']").TouchSpin({
verticalbuttons: true
});
</script>
答
尝试从int更改属性的类型为int?
+0
它不能为空。 –
您的手动代码不会为客户端验证生成必需的'data-val- *属性。它应该是'@ Html.TextBoxFor(m => m.Score,new {@class =“form-control”})。不要尝试设置'name'或'value'属性(并且使用'name =“Score”'dos无论如何)并且助手已经创建了'id =“Score”' –
感谢您的回复。你有什么建议来解决这个问题?我使用整数输入作为第二个组件,当它是空的我想验证并显示相关的错误信息。请帮忙吗? –
为什么不使用'TextBoxFor()'方法? –