【发布时间】:2012-06-21 06:47:57
【问题描述】:
我不能使用 EditorFor,因为我的输入还有一些其他属性,例如 readonly、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,无论任何值。
我哪里错了,或者我能做些什么来修复它?
【问题讨论】:
标签: c# asp.net-mvc-3 razor html-helper