srujana
srujana

Reputation: 67

How to format Decimal in Asp.Net Mvc3

I need to validate my editor field for entering the price of product like he can enter integer as 100,or after decimal only allows two digits as 85.12,and if user enters 85,12 also it is valid and it stores in database as 85.12. model class:

[Required]

[LocalizedDisplayName("PRICE_PER_COLLI", NameResourceType = typeof(Strings))] [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]

public decimal ColliPrice { get; set; }

View:

<%: Html.EditorFor(model => model.ColliPrice) %> 
<%: Html.ValidationMessageFor(model => model.ColliPrice)  

Upvotes: 0

Views: 372

Answers (1)

Liam
Liam

Reputation: 29634

I've done this before like:

@Html.TextBox("ColliPrice", Model.ColliPrice.ToString("0.00")) 
@Html.ValidationMessageFor(model => model.ColliPrice)

Upvotes: 1

Related Questions