I have implemented the MVC extension for formatting numbers in my application. It is based on the code found here . And it looks like this
public static MvcHtmlString DecimalBoxFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, double?>> expression, string format, object htmlAttributes = null) { var name = ExpressionHelper.GetExpressionText(expression); double? dec = expression.Compile().Invoke(html.ViewData.Model); var value = dec.HasValue ? (!string.IsNullOrEmpty(format) ? dec.Value.ToString(format) : dec.Value.ToString()): ""; return html.TextBox(name, value, htmlAttributes); }
When I call it with the next line of Razor syntax
@Html.DecimalBoxFor(model => Model.PointAttributes[i].Data.Y,"0.000", new { @class = "span1 number" })
I get an exception because the 'name' variable in my extension is an empty string. I tried to change the var name string to this, but it only gives me the property name βYβ and not the full βModel.PointAttributes [i] .Data.Yβ that I need to bind the model to MVC.
var name = ((expression.Body is MemberExpression ?((MemberExpression)expression.Body).Member : ((MemberExpression)((UnaryExpression)expression.Body).Operand).Member)).Name;
source share