2016-09-24 72 views
0

如何在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> 
+0

您的手動代碼不會爲客戶端驗證生成必需的'data-val- *屬性。它應該是'@ Html.TextBoxFor(m => m.Score,new {@class =「form-control」})。不要嘗試設置'name'或'value'屬性(並且使用'name =「Score」'dos無論如何)並且助手已經創建了'id =「Score」' –

+0

感謝您的回覆。你有什麼建議來解決這個問題?我使用整數輸入作爲第二個組件,當它是空的我想驗證並顯示相關的錯誤信息。請幫忙嗎? –

+0

爲什麼不使用'TextBoxFor()'方法? –

回答

-1

嘗試從int更改屬性的類型爲int?

+0

它不能爲空。 –