Make sure you perform this in the controller and not in the view when using MVC.