View is not the best place for application logic. 
Please move such logic to separate method/class.