2012-07-23 41 views
0

我認爲這應該很容易,但它困惑了我2天。模型不能解析字符串與組分隔符到長類型

我使用jquery.numberformatter-1.2.3.min.js來格式化和解析用戶輸入。

$("#Qty").blur(function() { 
    $(this).parseNumber({ format: "#,###", 
          locale: "us" }); 
    $(this).formatNumber({ format: "#,###", 
          locale: "us" }); 
}); 

我有一個模型字段顯示在視圖

[Required]   
public long? Qty { get; set; } 

<div class="display-field">@Html.TextBoxFor(model => model.Qty)</div> 

然而,在使用時輸入「243000」,該MVC3控制器動作總是認爲這是無效的長。

我試圖使用數據註釋格式屬性,沒有運氣。

[DisplayFormat(DataFormatString = "{0:#,###}", ApplyFormatInEditMode = true)] 

我怎樣才能讓MVC在我的動作中自動格式化用戶輸入並獲得模型狀態有效?

似乎jQuery numberformatter停止默認的MVC模型格式化程序來解釋字符串。

+0

這可能是一個語言環境問題。嘗試使用小數點來表示逗號,而不是逗號。 – 2012-07-24 21:52:58

回答

0

試試這個:

$("#Qty").blur(function() { 
    $(this).parseNumber({ format: "#.###", 
          locale: "us" }); 
    $(this).formatNumber({ format: "#.###", 
          locale: "us" }); 
}); 

[DisplayFormat(DataFormatString = "{0:#.###}", ApplyFormatInEditMode = true)] 

這可能是一個區域的問題。如果這解決了你的問題,讓我知道,我們會從那裏開始。

+0

thansk您的回覆。我試過我們或au,都得到qty = 0和modelState無效。我不知道我使用的js庫阻止了正常的MVC格式過程。 – ForeignEuphony 2012-07-25 02:15:28