【问题标题】:Call DisplayFormatAttribute's Logic from C#从 C# 调用 DisplayFormatAttribute 的逻辑
【发布时间】:2016-11-05 23:12:11
【问题描述】:

在我的 MVC5 应用程序中,我在模型的一个属性上设置了这个 DisplayFormat 属性。它确保我的双精度值“TotalPrice”始终在屏幕上以两位小数呈现(在 Razor 页面中)

[DisplayFormat(DataFormatString = "{0:N2}")]
public Double? TotalPrice { get; set; }

我试图弄清楚如何从我的控制器(C# 代码)中调用相同的逻辑。所以像:

return DataFormatAttribute.ApplyFormattingTo(shoppingCart.TotalPrice);

这显然远不正确,但我希望它有助于澄清问题。我对 2 位小数的具体示例并不感兴趣,更多的是如何在 C# 中自己将属性的行为应用于值。

我已经下载了 MVC5 源代码,但它对我来说太抽象了。

感谢您提供的任何帮助。

【问题讨论】:

  • 您是否打算将格式化后的值返回给视图或数据库/存储?我正在尝试理解其目的。
  • 只是编辑我的问题以尝试澄清。我需要在我的 C# 代码中做任何事情,无论 MVC5 框架在将值渲染到小数点后 2 位时为我做了什么。忍受我...
  • 你到底想要这个做什么?您只需要格式化显示在视图中的值(这是您的DisplayFormatAttribute 所做的) 为什么需要从我的控制器中调用相同的逻辑
  • 因为我有一种方法可以返回原始值,例如3.9 作为 ajax 请求的 Json 结果,因此它不会像模型值一样呈现。在我发回之前,我可以自己将它渲染到 3.90,但我希望能够连接到属性的功能。然后,如果我改变了显示逻辑,我只需要在一个地方改变它。另外,我对这些属性在幕后的实际工作方式很感兴趣。

标签: c# asp.net-mvc-5


【解决方案1】:

我不确定它是否可用 OOB,但看起来您需要阅读 Attribute 并应用您的格式。编写一个扩展方法,该方法将应用基于 DisplayFormatAttribute DataFormatString 属性的格式。

我希望这个示例可以帮助您入门,即使它不是一个精确的解决方案。此示例仅与 DisplayFormatAttribute 紧密绑定

    public static string ApplyFormattingTo(this object myObject, string propertyName) 
    {
        var property = myObject.GetType().GetProperty(propertyName);        
        var attriCheck = property.GetCustomAttributes(typeof(DisplayFormatAttribute), false); 
        if(attriCheck.Any())
        {   
             return string.Format(((DisplayFormatAttribute)attriCheck.First()).DataFormatString,property.GetValue(myObject, null)); 
        }
        return "";
    }

用法

Cart model= new Cart();
model.amount = 200.5099;    
var formattedString = pp.ApplyFormattingTo("amount");

实际实现会根据您的要求而有所不同。希望对您有所帮助。

【讨论】:

  • 谢谢,感谢您的帮助。我很高兴,但我会在可能的时候回复。干杯!
  • 太棒了,感谢@Searching!这完美地工作。我知道我正在做的可能没有遵循最佳实践,但在需要时能够做到这一点真的很有用。我将发布我的实际代码作为完整性的另一个答案。而且,我希望这种方法可以用来挂钩任何属性的功能,以避免重复代码。
【解决方案2】:

@Searching 提供了答案,所以这只是我的最终代码,以防将来对其他人有所帮助。 CartExtension 类为名为 Cart 的类提供扩展方法。

namespace MyApp.Models
{
    public static class CartExtension
    {
        /// <summary>
        /// Apply the formatting defined by the DisplayFormatAttribute on one of the Cart object's properties
        /// programatically. Useful if the property is being rendered on screen, but not via the usual Html.DisplayFor
        /// method (e.g. via Javascript or some other method)
        /// </summary>
        /// <param name="cart"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string ApplyFormattingTo(this Cart cart, string propertyName)
        {
            var property = cart.GetType().GetProperty(propertyName);
            var attriCheck = property.GetCustomAttributes(typeof(DisplayFormatAttribute), false);
            if (attriCheck.Any())
            {
                return string.Format(((DisplayFormatAttribute)attriCheck.First()).DataFormatString, property.GetValue(cart, null));
            }
            return "";
        }
    }
}

然后可以使用它返回为屏幕预先格式化的值,例如在控制器方法中:

return Json(cart.ApplyFormattingTo("TotalPrice"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2019-04-25
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2020-05-11
    • 2020-10-21
    相关资源
    最近更新 更多