2012-07-23 42 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