Wednesday 11 September 2013

ConvertEmptyStringToNull in MVC

When using MVC and model binding from a form post there comes a time when your database doesn't allow nulls but you find that any empty string from a form post comes back as null. This might be a PITA if you are directly saving the posted form back to the database and don't want to manually convert string null's to empty strings.

The default behaviour of the DefaultModelBinder, is that ConvertEmptyStringToNull is by default, set to true.

To get around this you can add an attribute to your property that guarantees that the binder will not convert the property to an empty string rather than null.

[DisplayFormat(ConvertEmptyStringToNull = false)]
public virtual string Language { get; set; }

Another way is to do it at a global level is to create your own model binder

public class EmptyStringModelBaseBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        bindingContext.ModelMetadata.ConvertEmptyStringToNull = false;

        return base.BindModel(controllerContext, bindingContext);
    }
}
and add your binder in global.asax
ModelBinders.Binders.DefaultBinder = new EmptyStringModelBaseBinder();

Thursday 22 August 2013

Set the default language for CKEditor and SCAYT

In order to set the DEFAULT language and spell checker in CKEditor when using SCATY (Spell check as you type) you need to add the following in your config values


CKEDITOR.editorConfig = function (config) {
   config.language = 'en-gb';
   config.wsc_lang = "en_GB";
   config.scayt_sLang = 'en_GB';
   config.scayt_autoStartup = true;

   ...
};

Note:-

- the language is en DASH gb (all lower)
- the wsc_lang and scayt_sLang is en UNDERSCORE upper GB

Monday 19 August 2013

Git and mark as assume-unchanged

I'm working on a small side-project that is on a public GIT repository and there are a few settings that should not be seen or used by other people.

Some settings that I do not want to show are:-

  • Connection strings
  • Meetup API keys

One great way to get around this is two first create the app.config and blank out the values you don't want. Then commit and push to your remote repository. After which key in the following command:-

git update-index --assume-unchanged [fileName]

Git will then stop monitoring changes to that file allowing you to put the real config info into it without fear of checking it in. If you later make changes that you DO want to check in you can run:

git update-index --no-assume-unchanged [fileName]

Lovely!

Thursday 27 June 2013

Generating a list of which files changed between hg versions

This is more of a note to myself so I can refer to it later!

Every time I want to see a list of changes between two Mecurial revisions I can never remember the correct syntax..

hg status --rev x:y

Where x and y are desired revision numbers...