Wednesday, June 27, 2012

Formatting data using Html.TextBoxFor asp.net MVC

If you've ever wanted to format values such as decimal or dates using the TextBoxFor helper, here's how.




@Html.TextBoxFor(m => m.YourProperty, new { @class = "MyClass", @Value = string.Format("{0:C}",Model.YourProperty) })




You can do this using EditorFor and the DisplayFormat annotation.  However, you loose the ability to add class to your textbox.

1 comment: