防爆pressionHelper.GetEx pressionText(如pression)没有返回我的属性的名称 [英] ExpressionHelper.GetExpressionText(expression) not returning the name of my property

查看:137
本文介绍了防爆pressionHelper.GetEx pressionText(如pression)没有返回我的属性的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个MVC扩展在我的应用程序来格式化数字。它是基于关闭code发现 href=\"http://stackoverflow.com/a/7766382/455492\">。与如下

 公共静态MvcHtmlString DecimalBoxFor<的TModel>(此的HtmlHelper<的TModel> HTML,防爆pression< Func键<的TModel,双>>前pression,串格式对象htmlAttributes = NULL)
{
    变量名称=前pressionHelper.GetEx pressionText(如pression);
    双? DEC =前pression.Compile()调用(html.ViewData.Model)。
    VAR值= dec.HasValue? (string.IsNullOrEmpty(格式)dec.Value.ToString(格式):dec.Value.ToString()!?):;
    返回html.TextBox(名称,价值,htmlAttributes);
}

在我的下面一行剃刀语法称之为

  @ Html.DecimalBoxFor(型号=> Model.PointAttributes [I] .Data.Y,0.000,新{@class =span1号})

我得到一个例外,因为在我的扩展变量'名'是一个空字符串。我曾试图改变变量名称行这一点,但它只是给我的属性名'Y',而不是完整的Model.PointAttributes [I] .Data.Y我需要的模型绑定回MVC。

<?pre> 变数名称=((如pression.Body是MemberEx pression((MemberEx pression)前pression.Body)。成员: ((MemberEx pression)((UnaryEx pression)前pression.Body).Operand)。成员))名称。


解决方案

这是一个已知的行为。我已经想通了,写我自己的版本<一href=\"https://github.com/rmariuzzo/Mariuzzo.Web.Mvc.Extras#ex$p$pssionhelper\"><$c$c>Ex$p$pssionHelper用于处理特定情况下。现在你有两个选择:


  1. 使用的NuGet包:

     安装封装Mariuzzo.Web.Mvc.Extras


  2. 或只是抢<一个href=\"https://github.com/rmariuzzo/Mariuzzo.Web.Mvc.Extras/blob/master/Mariuzzo.Web.Mvc.Extras/Ex$p$pssionHelper.cs\">the上述前pressionHelper 来源$ C ​​$ c和胶水到您的项目。


I have implemented a MVC Extension to format the numbers in my application. It is based off the code found here. And is as follows

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 following line of Razor syntax

@Html.DecimalBoxFor(model => Model.PointAttributes[i].Data.Y,"0.000", new { @class = "span1 number" })

I get an exception because the variable 'name' in my extension is an empty string. I have tried changing the var name line to this but it only gives me the property name of 'Y' and not the full 'Model.PointAttributes[i].Data.Y' that I need to bind the model back for MVC.

var name = ((expression.Body is MemberExpression ?((MemberExpression)expression.Body).Member : ((MemberExpression)((UnaryExpression)expression.Body).Operand).Member)).Name;

解决方案

This is a known behavior. I have figured out writing my own version of ExpressionHelper that handle that specific case. Now you have two option:

  1. Use the NuGet package:

    Install-Package Mariuzzo.Web.Mvc.Extras
    

  2. Or just grab the source code of the aforementioned ExpressionHelper and glue it into your project.

这篇关于防爆pressionHelper.GetEx pressionText(如pression)没有返回我的属性的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆