Data Annotation for currency format not working

南楼画角 提交于 2019-12-02 11:59:43

A [DisplayFormat] attribute is only respected when using @Html.DisplayFor() or @Html.EditorFor(). It is ignored when using TextBoxFor().

In addition, if you wanted to use it with @Html.EditorFor(r => r[i].SaleAmount) you need to modify the attribute to include the ApplyFormatInEditMode property

[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public float? SaleAmount { get; set; }

however that would be of little use to you, because although it would display in the textbox correctly, it will not bind back to you float property unless you were also to create a custom model binder which converted (say) "$15,481" back to a float

MANISH KUMAR CHOUDHARY

The currency annotation can be used. However it is just telling MVC which display or editor template to use. As We said current template uses the system currency. You would have to provide custom editor template or display template and some other way to determine the currency symbol to display.Look here at how to provide your own implementations

Try using this

 [DisplayFormat(DataFormatString = "{0:C0}")]

Example

public class Sales
{
    [Key]
    public int SalesId { get; set; }

    [DisplayFormat(DataFormatString = "{0:C0}")]
    public float? SaleAmount { get; set; }
}

Check here for more details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!