find a location for property in a new city

Thursday, 23 September 2010

Get all month names from DateTime in C# programatically

I needed to have a drop down list of all the months but I don't want to be writing all of that out. So is there a way to list all the months programmatically in C#. Hell yeah there is

I knew there would be a way to do it and I knew it would be something to do with the CultureInfo of the session.

Here is a method I wrote to return a set of key values for each month:
public IEnumerable<KeyValuePair<int, string>> GetAllMonths()
 for (int i = 1; i <= 12; i++)
  yield return new KeyValuePair<int, string>(i, DateTimeFormatInfo.CurrentInfo.GetMonthName(i));

Just to demonstrate, I used it in my ASP.NET MVC app:
<%: Html.DropDownListFor(model => model.DobMonth, new SelectList(Model.GetAllMonths(), "Key", "Value", Model.DobMonth)) %>

Which outputs the following HTML:
<select id="DobMonth" name="DobMonth">
<option selected="selected" value="1">January</option> 
<option value="2">February</option> 
<option value="3">March</option> 
<option value="4">April</option> 
<option value="5">May</option> 
<option value="6">June</option> 
<option value="7">July</option> 
<option value="8">August</option> 
<option value="9">September</option> 
<option value="10">October</option> 
<option value="11">November</option> 
<option value="12">December</option> 

Follow britishdev on Twitter

Bet you didn't know about: C# Numerical suffixes

I love learning new things and you probably do too. So how about this one... I found a way to make a long, double, unsigned int etc without having to use casting or having to explicitly declare the type.

Instead of using explicit declarations and casting like this:

long l = 12;
var d = (double)12;
float f = 12.0; //compilation error
var u = (UInt32)12;
decimal m = 12;
ulong ul = 12;

You can use the numerical suffixes like this:

var l = 12L;
var d = 12D;
var f = 12.0F;
var u = 12U;
var m = 12M;
var ul = 12UL;

So there we are. Interesting? Or maybe everyone already knew that and I've only just found it.

Follow britishdev on Twitter

Wednesday, 15 September 2010

Using Application State as early as possible in ASP.NET MVC

I'm working on a new website that will run off the same architecture as To distinguish one site from the other I am using a variable stored in ApplicationState. However I was running into problems when trying to access this variable.

The problem was that I was setting the Application variable in Application_Start() of Global.asax. I was accessing this variable in the contructor of my base Controller (that all my Controllers inherit from).

This was too early. Not too early for the Application_Start() event, obviously. Just too early to be able to access it I assume.

So how early can I access ApplicationState?

I found I could access it without problem on the Initialize method of my BaseController:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    //only here is the application state finally ready to be set
    SiteID = (int)requestContext.HttpContext.Application["SiteID"];

Problem is I really want to be able to access it during construction of my BaseController though since I will be implementing DI shortly. So in the end I used AppSettings in the config instead which can be accessed in the constructor. Still, this will still be useful I'm sure.

Follow britishdev on Twitter

Monday, 6 September 2010

How to force clients to hard refresh their browser cache

We had this problem when we removed our style sheets and put them into a HttpHandler to serve them all in one single request. The problem was that due to some caching problem, users' needed to refresh their page to receive the updated CSS files. This resulted in confused users looking at a non-styled site featuring trendy black Times New Roman font on a original white background with blue and purple retro links? old skool...

We (as developers) nonchalantly refreshed the page and happily continued, others didn't and we quickly realised that "Oh, they just need to refresh the page" wasn't good enough.


We needed a way to make the users refresh their browser caches, but how since we have no control over their browsers?

I came up with a bit of JavaScript that will do a hard refresh of the page and use cookies to record that it has been refresh and ensure that it only happens once and doesn't go into an infinite loop.

Here is some code I came up with:
//jquery plugin for cookies
<script type="text/javascript" src="/js/jquery/jquery.cookies.2.0.1.min.js"></script>
<script type="text/javascript">
//give it a new name each time you need to do this
var cookieName = 'refreshv1';
//check client can use cookies
if ($.cookies.test()) {
    //get the cookie
    var c = $.cookies.get(cookieName);
    //if it doesn't exist this is their first time and they need the refresh
    if (c == null) {
        //set cookie so this happens only once
        $.cookies.set(cookieName, true, { expires: 7 });
        //do a "hard refresh" of the page, clearing the cache
Feel free to work with this code but please do not use it without testing or ensuring it is right for your situation. It is merely an idea that worked for me.

Follow britishdev on Twitter