0

I'm trying to make a more customized data annotation for one of attributes. In my data model, I have an attribute:

public int AutoCallableStart { get; set; }

And I want to display it like this: 1 --> "1st" 2 --> "2nd" 3 --> "3rd", ..., etc.

In a view model implementation (my current implementation), I have:

public string CallFrom
    {
        get
        {
            switch (_callableIncome.AutoCallableStart)
            {
                case 1:
                    return "1st";
                case 2:
                    return "2nd";
                case 3:
                    return "3rd";
                case 4:
                    return "4th";
                case 5:
                    return "5th";
                default:
                    return "???";
            }

        }
    }

Can this be done using data annotations such as something like

[Display(Name="CallFrom")
[TypeConverter(Type=".....")]

using a TypeConverter? I've googled everywhere but can't find anything. I want to take full advantage of EF and scrap my view models.

Thanks for your help.

4
  • might be easier to just create a displaytemplate
    – JamieD77
    Sep 8, 2016 at 14:11
  • "I want to take full advantage of EF and scrap my view models." - Its a trap! Seriously don't do this, it will bite you in the arse eventually. At best you will end up with a mix of EF 'view/data models' and special case view models all mixed together. DataModel != ViewModel
    – Paul Swetz
    Sep 8, 2016 at 14:20
  • Second display templates. You can create on like Ordinals.cshtml and then simply decorate the property with [UIHint("Ordinals")]. Also, "I want to take full advantage of EF and scrap my view models" is just folly. You entity classes should only focus on the needs of the database. Anything dealing with display or such should be on a view model. View models are virtually a necessity. If you try to just use entities, your app will be brittle and more error prone. Sep 8, 2016 at 14:21
  • OK. So another approach you guys are saying is just keep my view models and can the annotations? Are you saying annotations break the model/viewmodel pattern I am currently using? Thanks! Sep 8, 2016 at 14:32

1 Answer 1

1

Based on the comments, I've decided to make a DisplayTemplate. I've also kept the view models. Here's my display template: \Shared\DisplayTemplates\CallFrom.cshtml

@model int

@{
switch (@Model)
{
    case 1:
        <span>1st</span>
        break;
    case 2:
        <span>2nd</span>
        break;
    case 3:
        <span>3rd</span>
        break;
    case 4:
        <span>4th</span>
        break;
    case 5:
        <span>5th</span>
        break;
    default:
        <span>???</span>
        break;
}
}

And my annotation in my VIEW MODEL

[Display(Name="CallFrom")]
[UIHint("CallFrom")]
public int AutoCallableStart
{
    get { return _callableIncome.AutoCallableStart; }
}

Works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.