1

My application is in asp.Net MVC3 coded in C#.Net.

I want to truncate the decimal part of my values when they are displayed in the TextBoxes. The DataType for these values are set to Decimal in the database so even if i save 150 it saves as 150.0000.And when it displays a particular record it comes back as 150.0000 but i dont want it that way,i only want 150.

I have tried

  @Html.TextBoxFor(string.Format("Decimal Truncation Format", model => model.myValue))

But not working..The values are travelling from many views. So can i have such a truncation in the model itself so that it applied to that value all the time.

If not possible that way then how can i achieve it.?

2 Answers 2

2

Why don't you modify the property in your ViewModel to Int32? That should solve your problem in an instant!

EDIT:

This assumes that you have a ViewModel. You should never bind views directly to your db, but to a view-specific model (the ViewModel). Then you can modify that ViewModel as much as you want, so that it serves the needs of the View rather than those of the DB.

Check out the following links:

Good luck!

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the reply.But the phase in which my project is i cant afford to change the Database Definition,.
You don't have to change the DB definition, just the ViewModel.
i have changed the Decimal To Int32 in the model.And i gets this error. The type 'Edm.Decimal' of the member 'MYValue' in the conceptual side type 'MyModel.MyClass' does not match with the type 'System.Int32' of the member 'MYValue' on the object side type 'MyApp.Models.MYClass'. And if i change it to Int32 lot Linq queries in my Application are throwing error.
Are you binding directly to your EDMX model?
Ah, that's the problem. You should never bind views directly to your db, but to a view-specific model (the ViewModel). Then you can modify that ViewModel as much as you want, so that it serves the needs of the View rather than those of the DB.
0

Another solution: use TextBox instead of TextBoxFor, which let you initialize the field with any value.

@Html.TextBox("myValue",String.Format("{0}", Model.myValue)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.