I have an array of double in my View Model and I'd like to apply a data format string like shown below:
class MyVM
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:F2}")]
public double Currents[] { get; set; }
}
In my View, I am using EditorFor on the array like shown below, which is properly working except its not applying the data format string.
@Html.EditorFor(model => model.Currents)
I learned that I can use custom templates, but was wondering is there is a simpler way than creating a custom template. If custom template is the only solution, what is the simplest custom template that should take care of the formatting? I tried creating one but was not successful in applying the data format string properly.
I can create a CurrentsTemplate.cshtml in the ~Views\Shared folder with the below content
@model double[]
@foreach (double x in Model) {
<div class="editor-field">
@Html.TextBoxFor(m=>x, "{0:F2}")
</div>
}
and use that by calling @Html.EditorFor(m => m.Currents,"CurrentsTemplate"), it will work but the form data is not persisting after its POSTed. Guess I need hidden fields in the for loop along with the TextBox.