我有以下的模型類(剝離爲簡單起見):ASP.NET MVC默認聯:太長整數,空驗證錯誤消息
public class Info
{
public int IntData { get; set; }
}
這裏是我的剃刀形式使用此模型:
@model Info
@Html.ValidationSummary()
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.IntData)
<input type="submit" />
}
現在,如果我在文本框中輸入非數字數據,則會收到正確的驗證消息,即:「值'qqqqq'對於'IntData'字段無效」。
但是,如果我輸入很長的數字序列(如345234775637544),我會收到一個EMPTY驗證摘要。
在我的控制器代碼,我看到ModelState.IsValid
是false
預期,並且ModelState["IntData"].Errors[0]
如下:
{System.Web.Mvc.ModelError}
ErrorMessage: ""
Exception: {"The parameter conversion from type 'System.String' to type 'System.Int32' failed. See the inner exception for more information."}
(exception itself) [System.InvalidOperationException]: {"The parameter conversion from type 'System.String' to type 'System.Int32' failed. See the inner exception for more information."}
InnerException: {"345234775637544 is not a valid value for Int32."}
正如你所看到的,確認工作正常,但不會產生一個錯誤信息用戶。
我可以調整默認模型聯編程序的行爲,以便在此情況下顯示正確的錯誤消息嗎?或者我將不得不編寫一個自定義聯編程序?
謝謝,如果我無法調整默認綁定器,我會選擇此解決方案。 – Zruty 2011-06-09 09:11:33
如果你想通過屬性'[Display(Name = ...)]'來獲得本地化的fieldname,''我建議把'bindingContext.ModelName'改爲'bindingContext.ModelMetadata.DisplayName'。 – Gh61 2015-03-31 13:38:06