locked
Data Formatting Strings RRS feed

  • Question

  • User-1422983141 posted

    I always have the issue of poorly formatted data for strings. I know there are many ways to do it but I only want it in one place

    I would like to be able to do upper, camel, or lower case from the model as annotations or set it in the application Context to have SQL server do the work.

    I looking for the cleanest and easiest way to do this when the user couldn't care less and types anyway they want and before it gets stored.

    I'm wondering if there is a better way or just creating a class to perform this in the model|? 

    Tuesday, September 17, 2019 1:35 AM

All replies

  • User1724605321 posted

    Hi kvic ,

    One option is write custom model binding function which write your custom logic to format string . Below is one of the code sample :

    https://www.c-sharpcorner.com/article/custom-model-binding-in-asp-net-mvc/

    Best Regards,

    Nan Yu

    Wednesday, September 18, 2019 2:53 AM
  • User-1422983141 posted

    I have extended my model and now seeing upper and propercase being applied from the model.

    One place to make all changes to the model seems to be the cleanest option 

     [MaxLength(128)]
            [Required(AllowEmptyStrings = false, ErrorMessage = "Company Name is Required.")]
            [Display(Name = "Company Name", Description = "The Company Name.", Prompt = "Company Name", ShortName = "Company", Order = 1)]
            public string CompanyName
            {
                get
                {
                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    return textInfo.ToTitleCase(_companyName.ToLower());
                }
                set
                {
                    _companyName = CompanyName;
                }
            }
    
            private  string _ticker;
    
            [MaxLength(128)]
            [Required(AllowEmptyStrings = false, ErrorMessage = "Company Ticker is Required.")]
            [Display(Name = "Ticker", Description = "The Registered Company Ticker.", Prompt = "Company Ticker", ShortName = "Ticker", Order = 2)]
            public string Ticker
            {
                get
                {
                    return _ticker.ToUpper();
                }
                set
                {
                    _ticker = Ticker;
                }
            }

    Still intersted in other ideas or to make mine better

    Thursday, September 19, 2019 1:47 AM