Если у вас нет настроенного шаблона редактора для типа Decimal
, тогда EditorFor
, украшенный DisplayFormatAttribute
, вероятно, будет работать из коробки.
Для пользовательского шаблона редактора я в конечном итоге использовал что-тонапример:
@model decimal?
@{
string displayValue;
if (Model == null)
{
displayValue = null;
}
else {
var formatString = (ViewData.ModelMetadata).DisplayFormatString;
displayValue = formatString == null ? Model.ToString() : string.Format(formatString, Model);
}
}
<div class="form-group">
@Html.LabelFor(c => c)
@Html.TextBoxFor(c => c, new { type = "text", Value = displayValue, @class = "form-control" })
@Html.ValidationMessageFor(c => c)
</div>
, который работает, когда свойство украшено DisplayFormatAttribute
, например:
[DisplayFormat(DataFormatString = "{0:n1}", ApplyFormatInEditMode = true), Display(Name = "Commission")]
public decimal? CommissionPercentage { get; set; }