I can’t use EditorFor because my inputs have some other attributes like readonly, disable and class therefor i am using an extension for TextBoxFor. I need to display formatted numeric value so my extension method is defined as
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 is defined as "{0:#,##0.00##########}".
Lets say _value is 2, newHtmlAttrib shows it as 2.00 but the resultant html shows 0, it always shows 0 regardless of any value.
Where am i wrong or what can i do to get it fixed?
You should use the
TextBoxhelper if you want to specify a format: