C# 调用DisplayFormatAttribute';C语言的s逻辑#

C# 调用DisplayFormatAttribute';C语言的s逻辑#,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,在我的MVC5应用程序中,我在模型的属性上设置了这个DisplayFormat属性。它确保我的双值“TotalPrice”在屏幕上始终显示两位小数(在剃须刀页面中) 我正试图找出如何从我的控制器(C代码)中调用相同的逻辑。比如: 返回DataFormatAttribute.ApplyFormattingTo(shoppingCart.TotalPrice) 这显然是不正确的,但我希望这有助于澄清问题。我对小数点后两位的具体示例并不感兴趣,更多的是如何在C#中自己将属性的行为应用于值 我已经下载了

在我的MVC5应用程序中,我在模型的属性上设置了这个DisplayFormat属性。它确保我的双值“TotalPrice”在屏幕上始终显示两位小数(在剃须刀页面中)

我正试图找出如何从我的控制器(C代码)中调用相同的逻辑。比如:

返回DataFormatAttribute.ApplyFormattingTo(shoppingCart.TotalPrice)

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

我已经下载了MVC5源代码,但它对我来说太抽象了,我无法理解


感谢您提供的帮助。

我不确定是否有可用的OOB,但您似乎需要阅读
属性
并应用格式。编写一个扩展方法,该方法将基于
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");

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

我不确定是否有可用的OOB,但看起来您需要阅读
属性并应用格式。编写一个扩展方法,该方法将基于
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");

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

@search提供了答案,因此这只是我的最后一段代码,以防将来对其他人有所帮助。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 "";
        }
    }
}

@搜索提供了答案,所以这只是我最后的代码,以防它在将来帮助其他人。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 "";
        }
    }
}

是否要将格式化值返回到视图或数据库/存储?我试图理解目的。只是编辑我的问题,试图澄清。我需要在我的C#代码中执行MVC5框架在将值呈现为小数点后2位时为我执行的任何操作。容忍我…你到底想要这个干什么?您只需要格式化视图中显示的值(这是您的
DisplayFormatAttribute
所做的),为什么您需要从我的控制器中调用相同的逻辑?因为我有一个方法返回原始值,例如3.9,作为ajax请求的Json结果,所以它不会被呈现为模型值。在发回之前,我可以自己将它渲染到3.90,但我希望能够连接到属性的功能中。如果我改变了显示逻辑,我只需要在一个地方改变它。此外,我还对属性在后台的实际工作方式感兴趣。您是想将格式化的值返回到视图还是数据库/存储?我试图理解目的。只是编辑我的问题,试图澄清。我需要在我的C#代码中执行MVC5框架在将值呈现为小数点后2位时为我执行的任何操作。容忍我…你到底想要这个干什么?您只需要格式化视图中显示的值(这是您的
DisplayFormatAttribute
所做的),为什么您需要从我的控制器中调用相同的逻辑?因为我有一个方法返回原始值,例如3.9,作为ajax请求的Json结果,所以它不会被呈现为模型值。在发回之前,我可以自己将它渲染到3.90,但我希望能够连接到属性的功能中。如果我改变了显示逻辑,我只需要在一个地方改变它。另外,我对属性在引擎盖下的实际工作方式很感兴趣。谢谢,感谢您的帮助。我在任务单上很抱歉,但我会在可能的时候发回。干杯太棒了,谢谢@search!这很好用。我知道我所做的可能不是遵循最佳实践,但在需要时能够做到这一点是非常有用的。为了完整起见,我将发布我的实际代码作为另一个答案。而且,我希望这种方法可以用于连接任何属性的功能,以避免重复代码。谢谢,感谢您的帮助。我在任务单上很抱歉,但我会在可能的时候发回。干杯太棒了,谢谢@search!这很好用。我知道我所做的可能不是遵循最佳实践,但在需要时能够做到这一点是非常有用的。为了完整起见,我将发布我的实际代码作为另一个答案。而且,我希望这种方法可以用于连接任何属性的功能,以避免重复代码。