In my view I have the following call.
<%= Html.EditorFor(x => x.Cost) %>
I have a ViewModel with the following code to define Cost
This is the appropriate way if you are using EditorFor templates.
What does "inordinately inelegant" mean?
[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
public decimal Cost { get; set; }
and in your view:
<%= Html.EditorFor(x => x.Cost) %>
and that's all.
You will probably want to apply a custom CSS class. You could do this:
<div class="currency">
<%= Html.EditorFor(x => x.Cost) %>
</div>
and then have in your css:
.currency input {
/** some CSS rules **/
}
or write a custom DataAnnotationsModelMetadataProvider which will allow you to:
[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
[HtmlProperties(CssClass = "currency")]
public decimal Cost { get; set; }
and then in your view:
<%= Html.EditorFor(x => x.Cost) %>