C# 从服务器端获取DataFormatString格式的值

C# 从服务器端获取DataFormatString格式的值,c#,model-view-controller,data-annotations,displayformat,C#,Model View Controller,Data Annotations,Displayformat,当然,它在视图中起作用,结果是“10000英镑” 我如何通过服务器端检索格式化的值 @Html.DisplayFor(model => report.MarketValue) 提前谢谢 非常粗糙,但它可以做到: string formattedValue = report.MarketValue.ToFormattedString(); 我认为否决票应该发表评论。理想情况下,每一次批评都应该是建设性的。 string formattedValue = report.MarketValu

当然,它在视图中起作用,结果是“10000英镑”

我如何通过服务器端检索格式化的值

@Html.DisplayFor(model => report.MarketValue)

提前谢谢

非常粗糙,但它可以做到:

string formattedValue = report.MarketValue.ToFormattedString();

我认为否决票应该发表评论。理想情况下,每一次批评都应该是建设性的。
string formattedValue = report.MarketValue.ToFormattedString();
public static string GetAttributeFormatedString<TModel, TMember>(this TModel model, Expression<Func<TModel, TMember>> memberSelector)
{
    var selectorBody = memberSelector.Body;
    if (selectorBody.NodeType != ExpressionType.MemberAccess)
    {
        throw new ArgumentException("Nope dude, not this time", "memberSelector");
    }
    var attribute = ((MemberExpression) selectorBody).Member
        .GetCustomAttributes(typeof(DisplayFormatAttribute), true)
        .OfType<DisplayFormatAttribute>()
        .FirstOrDefault();
    if (attribute == null)
    {
        throw new InvalidOperationException("Attribute, dude");
    }
    var format = attribute.DataFormatString;
    var result = string.Format(format, memberSelector.Compile().Invoke(model));

    return result;
}
    var r = new Report
    {
        MarketValue = 10000
    };
    var formated = r.GetAttributeFormatedString(x => x.MarketValue);