readonly入力にのような他の属性があるため、EditorForを使用できませんdisable。classそのため、TextBoxForの拡張機能を使用しています。フォーマットされた数値を表示する必要があるため、拡張メソッドは次のように定義されます。
public static MvcHtmlString FieldForAmount<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression)
{
MvcHtmlString html = default(MvcHtmlString);
Dictionary<string, object> newHtmlAttrib = new Dictionary<string, object>();
newHtmlAttrib.Add("readonly", "readonly");
newHtmlAttrib.Add("class", "lockedField amountField");
var _value = ModelMetadata.FromLambdaExpression(expression,
htmlHelper.ViewData).Model;
newHtmlAttrib.Add("value", string.Format(Formats.AmountFormat, value));
html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,
expression, newHtmlAttrib);
return html;
}
Formats.AmountFormatとして定義され"{0:#,##0.00##########}"ます。
_valueが2である とすると、newHtmlAttribとして表示されますが2.00、結果htmlは表示され0、値に関係なく常に表示され0ます。どこが間違っているのですか、それを修正するために何ができますか?