The main problem that I encountered is that I needed to create a mechanism like EditorFor to format the decimal as a currency (our system has several currencies, so "C" would not be suitable), get a tab index that works And allow the system to support standard validation.
I managed to achieve this using the following. By creating your own custom editor control.
Create a file (mine is called decimal.ascx) in the Views / Shared / EditorTemplates directory of your project.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %> <% int intTabindex = 0; decimal myVal = 0; string strModelValue = ""; if (Model != null) { myVal = (decimal)Model; strModelValue = myVal.ToString("#.00"); } else strModelValue = ""; if (ViewData["tabindex"] != null) { intTabindex = (int)ViewData["tabindex"]; } %> <%: Html.TextBox("", strModelValue, new { @tabindex = intTabindex })%>
Essentially, this code simply overrides what would normally be represented in the "decimal" editor for a method with <; p>
<%: Html.TextBox("", Model.ToString("#.00"), new { @tabindex = intTabindex }) %>
template.
My calling code is now readable;
<%: Html.EditorFor(model => Model.MyItem, new { tabindex = 5 })%>
The result is the following code on the page.
<input id="Model_MyItem" name="Model.MyItem" tabindex="5" type="text" value="12.33" />
This is exactly what I need.
Although this is true only for my specific circumstances, I would urge anyone who wants to solve this problem to first try to create their own control over the task, since this can save you a significant amount of time.
If in the code, of course, it will be possible to create a certain type of control and tune the results around it.
For instance; we could just add another element to the call to define the text format.
new {tabindex = 12, numberformat=2}
Then just create a handler for all formats.