I have to pages that need to show the item amount with currency format. The first one is where currency is entered and the second one where currency is displayed.
I would like the EditorFor to show an R indicated Rands and then i would like the value to be decimal.
Here is my EditorFor:
<div class="editor-field">
@Html.EditorFor(model => model.TransactionModel.Price)
</div>
I have tried many different ways and can't get any to work.
With this example below it does not know what 'CurrencyPrice' is in the second line.
var CurrencyPrice = '@Model.TransactionModel.Price';
document.getElementById('TransactionModel.Price').value = '@string.Format("{0:C}", CurrencyPrice)';
I have also tried these in my transactionModel:
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
//[UIHint("Currency")]
//[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
//[DataType(DataType.Currency)]
public decimal Price { get; set; }
Could someone please tell me how i can get this right?
Right now i will accept any working method.
With MVC 4, you can use the TextBoxFor method that includes a format string:
In MVC 3 and below:
EditorFor
might or might not work. Personally, I have had problems with it. For more inspiration, see this answer.I could not get the above to work for me. This is the solution I came up with that worked using Data Annotation on the Model.
I got the RegEx from JohnM here.
This should REALLY not be this hard for everyone! I was very surprised I had to put forth so much effort on validating currency in an ASP.NET application!!! What should have taken 30 seconds, took hours of research and testing.