在MVC 3个属性 [英] Percentage properties in MVC 3

查看:134
本文介绍了在MVC 3个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很多型号,其中许多包含百分比数据。这些被重新psented为 $ P $小数小数?模型结构。然而,并非与小数结构所有属性的百分比。一些应该像对待普通小数。

My application has many models, many of which contain percentage data. These are represented as decimal or decimal? structs in the model. However, not all properties with decimal structs are percentages. Some should be treated like regular decimals.

百分比需要特别注意:


  • 对于显示,他们应该使用 {0:P2} 格式。 (我有这部分工作。)

  • 为编辑,应允许相同的格式显示,即,95或95%或95.00%的所有绑定到0.95的值。

  • For display, they should use {0:P2} format. (I have this part working.)
  • For editing, they should allow the same format as the display, i.e. "95" or "95%" or "95.00 %" all bind to a value of 0.95.

我开始沿着创造的道路上 PercentModelBinder 实现 IModelBinder ,但后来意识到,你只能申请在 ModelBinderAttribute 的,而不是一个属性。

I started down the road of creating a PercentModelBinder that implements IModelBinder, but then realized that you can only apply the ModelBinderAttribute to a class, not a property.

什么是处理这种情况的最好办法部分的(但不是全部)类型的使用需要进行特殊处理都显示和约束力?

What's the best way to handle this case where some (but not all) uses of a type need special handling both for display and binding?

每一个解决方案,我想到的气味严重矫枉过正,战斗的MVC框架。当然还有一个更好的方法比:

Every solution I think of smells badly of overkill, fighting the MVC framework. Surely there is a better way than:


  • 创建一个自定义的百分比结构,并用它作为IModelBinder和EditorTemplates,或
  • 的基础
  • 重新实现的默认绑定行为小数小数?基于成竹在胸改变解析逻辑我模型或

  • 实现为每个类的自定义模型绑定包含一个百分比属性,或者

  • 在模型中使用假冒代理属性(即打破MVC)

  • Creating a custom Percentage struct and using it as the basis for the IModelBinder and EditorTemplates, or
  • Reimplementing the default binding behavior of decimal and decimal? and changing the parsing logic based on intimate knowledge of my model, or
  • Implementing a custom model binder for each class that contains a percentage property, or
  • Using fake proxy properties in the model (i.e. breaking MVC)

推荐答案

一种可能性是写一个自定义元数据属性的认识:

One possibility is to write a custom metadata aware attribute:

public class PercentageAttribute : Attribute, IMetadataAware
{
    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues["percentage"] = metadata.EditFormatString;
    }
}

然后是装饰重新present百分比与它的视图模型属性:

then decorate your view model properties that represent percentages with it:

public class MyViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:P2}")]
    [Percentage]
    public decimal? Percentage { get; set; }
}

和该值的presence自定义模型绑定测试里面:

and inside the custom model binder test for the presence of this value:

public class PercentageModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelMetadata.AdditionalValues.ContainsKey("percentage"))
        {
            var format = (string)bindingContext.ModelMetadata.AdditionalValues["percentage"];
            // TODO: do the custom parsing here
            throw new NotImplementedException();
        }
        else
        {
            // Let the default parsing occur
            return base.BindModel(controllerContext, bindingContext);
        }
    }
}

现在您可以将此模型绑定注册的所有小数。

Now you can register this model binder to all decimals.

这篇关于在MVC 3个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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