1

Given a domain object:

class BirthdayDomain
{
    public DateTime Birthday { get; set; }

    public decimal BirthdayPresent { get; set; }
}

I have two options for passing this to a strongly-typed view:

1.

class BirthdayView
{
    public DateTime Birthday { get; set; }

    public decimal BirthdayPresent { get; set; }
}

and in the view

<%: Model.Birthday.ToString("d"); %>
<%: Model.BirthdayPresent.ToString("C2"); %>

2.

class BirthdayView
{
    public string Birthday { get; set; }

    public string BirthdayPresent { get; set; }
}

and in the controller (for instance)

BirthdayDomain bd = Repository.GetBirthday(.....)
BirthdayView bv = new BirthdayView()
                  {
                       Birthday = bd.Birthday.ToString("d");
                       BirthdayPresent = bd.BirthdayPresent.ToString("C2");
                  }

and in the view just output the strings.

My question is this: if I want to support the user's (browser's?) current locale settings so that dates and currencies are displayed the way theu expect, where is the best place to do this? Can it be done in either the view or the controller? What is the generally-accepted technique for handling this?

3 Answers 3

2

If you want to support browser language put this in your web.config:

<globalization culture="auto" />

And in your view:

<%= Html.Encode(DateTime.Now.ToString()) %>

ACCEPT-LANGUAGE request header will be used to set the culture and format the date accordingly.

3
  • Ahhah! Thanks will give that a shot. Marking as answered (unless it doesn't work :))
    – Jedidja
    Mar 4, 2010 at 12:51
  • That would mean we definitely want to keep the original data type in the View Model (which is what I was leaning towards).
    – Jedidja
    Mar 4, 2010 at 12:55
  • Works just as expected. Awesome :)
    – Jedidja
    Mar 4, 2010 at 13:08
0

Typically you want want to do all your string manipulation in your model. When your controller instantiates a new model, pass it the local information so the model can properly format the date for you. Another option would be be to have the model pull the local information out of the context of the request. Just be sure to get your logic into the model.

0

I put culture formatting in the view because in my opinion it is its role and the data sent by the controller stays strong typed and not becomes basic string. At least for the auto-completion in your dev environment it is a bonus...

EDIT

You can also change the current Thread UI culture in your controller, if you do not want to specify whuich culture to use every time there is a ToString in your views.

3
  • So along with Birthday.ToString("d"); %> do you pass some sort of CultureInfo? I'm wondering how I get the current user's locale settings as well.
    – Jedidja
    Mar 3, 2010 at 20:41
  • @Jedidja: usually the culture info is part of my route data: ex: server/myapp/fr-FR/mycontroller/myaction/id and the controller hydrates it in one of my base custom view model porperty named "Culture" so in my view it would be Birthday.ToString(Model.Culture)
    – Gregoire
    Mar 3, 2010 at 21:23
  • Ohh..we definitely won't have that. I'm guessing there must be some way to grab that from the Request as well.
    – Jedidja
    Mar 3, 2010 at 21:31

Your Answer

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

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