在视图MVC 5中显示1000逗号分隔符

问题描述 投票:0回答:1

我确实有以下MVC视图元素,我试图在没有decimal的1000分隔符中显示数据。我尝试在数据注释中应用“{0:C0}”,“{0:N0}”(选项1),这样当数据是从数据库中提取并在视图中显示数据注释将被应用。它不起作用

Eq:1000000 => 1,000,000

选项1

[Display(Name = "Services", ResourceType = typeof(Resources.Labels))]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:C0}")]
            public int? Services { get; set; }

作为选项2,我在下面的代码片段中尝试以1,000,000格式显示拉取的数据,但在post方法中,字段值始终为null。任何输入,其中数据可以以1,000,000的格式显示,但是当它发布到后端时,它仍然应该将值传递为1000000。

选项2

<div class="notified-option-value cell border-top border-right" id=@("value-container" + i)>
                        @Html.TextBoxFor(m => m.OptionsEditViewModel.AddCoverageBBR[k].AddCoverage.Services, "{0:C0}", new { id = "limit-of-coverage-value" + i, @class = "option-value cell" })
                    </div>
javascript asp.net-mvc model-view-controller asp.net-mvc-5.2
1个回答
0
投票

对于这种情况,您有两种选择。

  1. 在JavaScript中处理格式化(1000 - > 1,000)和解析(1,000 - > 1000),

设置输入以键入文本并使用onbluronchangeonkeyup等输入事件格式化内容,有一些jQuery库可以处理这个问题。

  1. 将模型字段设为字符串,因此当您发送类似1,000,000的内容将被接受(而不是null)并在C#代码中处理它时,在这些情况下使用带有getter和setter的属性可能会有所帮助。
© www.soinside.com 2019 - 2024. All rights reserved.