Skip to content Skip to sidebar Skip to footer

Asp.net Mvc 4 Save 3 Decimal Place Value To Sql Server 2005

I have to set 3 decimal place in SQL Server 2005.i can set it from only model part .But In sql server set datatype of Salary property is decimal. When I set like public

Solution 1:

I also got stuck in the same situation and finally found the solution. You can override the DbContext.OnModelCreating() method and configure the precision

public class ApplicationDbContext : IdentityDbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity().Property(c => c.AssociationFee).HasPrecision(18, 3);
    }
}

Hope this helps

Solution 2:

Apply this attribute to the property in your model:

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

Solution 3:

You could use the MVC DisplayFormat Attribute:

DisplayFormat not getting applied on decimal value

Or for display purposes you could use a string and do:

Salary.ToString("F");

Which would result in the output: 30000.75

Or:

Salary.ToString("N");

Which would result in the output: 30,000.75

Solution 4:

Try this one , you have to set in edit mode also .

 [DisplayFormat(DataFormatString = "{0:n3}", ApplyFormatInEditMode = true)]
 publicdecimal Amount { get; set; }

Post a Comment for "Asp.net Mvc 4 Save 3 Decimal Place Value To Sql Server 2005"