Percentage properties in MVC 3

前端 未结 3 1189
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 19:56

My application has many models, many of which contain percentage data. These are represented as decimal or decimal? structs in the model. However, not

相关标签:
3条回答
  • 2021-02-08 20:40

    Bind to a string in your view model. Then when do the conversion back to the real model.

    0 讨论(0)
  • 2021-02-08 20:49

    Instead of representing your percentage properties using decimal primitives (see Primitive Obsession), why don't you create a Percentage type to wrap your desired functionality? You should have much more flexibility doing it that way...

    0 讨论(0)
  • 2021-02-08 20:54

    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;
        }
    }
    

    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; }
    }
    

    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.

    0 讨论(0)
提交回复
热议问题