The html helper methods check the ViewDataDictionary for a value. The value can either be in the dictionary or in the Model, as a property. To extract the value, an internal sealed class named the ViewDataEvaluator uses PropertyDescriptor to get the value. Then, Convert.ToString() is called to convert the object returned to a string.
Desired code in Controller action
The controller action should only populate the Model, not format it (formatting the model is global).
Desired code in View
The view can render a HTML textbox and extract the string representation of the property with this line of code:
<%=Html.TextBox(“Date”) %> <%=Html.TextBox(“Time”) %> <%=Html.TextBox(“UnitPrice”) %>
Binding Model's Property to HtmlHelper.TextBox()
For the textbox’s value, the UnitPrice property’s value from the model instance is converted to a string. I need to override this behavior with my own conversion to a string, which is per property – not per type. For example, I need a different string representation of a decimal for UnitPrice and another string representation of a decimal for UnitQuantity.
For example, I need to format the UnitPrice's decimal precision based on the market.
string decimalPlaces = ViewData.Model.Precision.ToString (); <%=Html.TextBox(“UnitPrice”, ViewData.Model.TypeName.UnitPrice.ToString("N" + decimalPlaces)) %>
2-way databinding please
Just like the IModelBinder is the Parse for each property of the model, I need a Format for each property, kinda like Windows Forms binding, but based on the model instead of the control. This would enable the model to round-trip and have proper formatting. I would prefer a design where I could override the default formatting. In addition, my model is in a separate assembly, so attributed properties specifying a formatter are not an option.
Please note I need property specific formatting for a model, not type specific formatting.