/// <summary>
        /// Called when the user selects a taxon in the tree
        /// The tree model in Session is updated with which nodes is expanded
        /// and which node is selected.
        /// Then the user is redirected to the requested page
        /// </summary>
        /// <param name="navigateId"></param>
        /// <param name="expandedNodes"></param>
        /// <returns></returns>
        public ActionResult TaxonTreeNavigate(int navigateId, string expandedNodes)
        {
            // navigateId is a unique sequential number and is not the TaxonId.
            // you get the TaxonId by model.GetTreeViewItem(navigateTaxonId).TaxonId.
            var model   = Session["TaxonTree"] as TaxonTreeViewModel;
            int taxonId = 0;

            if (model != null && !string.IsNullOrEmpty(expandedNodes))
            {
                TaxonTreeViewItem treeItem = model.GetTreeViewItemById(navigateId);
                if (treeItem == null) // happens when the user navigates backwards and the tree is different from the one in session
                {
                    RedrawTree(taxonId);
                }
                else
                {
                    taxonId = treeItem.TaxonId;
                    var   javascriptSerializer = new JavaScriptSerializer();
                    int[] expandedNodesList    = javascriptSerializer.Deserialize <int[]>(expandedNodes);
                    model.ActiveId     = navigateId;
                    model.ExpandedTaxa = expandedNodesList.ToList();
                    model.BuildParentTaxaList(treeItem);
                }
            }

            return(RedirectToAction(this.NavigateData.Action, this.NavigateData.Controller, new { @taxonId = taxonId }));
        }
        /// <summary>
        /// Trigger a refresh of the tree next time it is rendered.
        /// Tries to remember which taxa is expanded and which taxon is selected
        /// </summary>
        /// <param name="newRootTaxonId">root taxon in tree</param>
        /// <param name="selectedTaxonId">selected taxon</param>
        protected void RedrawTree(int newRootTaxonId, int selectedTaxonId)
        {
            var model = Session["TaxonTree"] as TaxonTreeViewModel;

            Session.Remove("TaxonTree");
            if (model != null && this.RootTaxonId.GetValueOrDefault() == newRootTaxonId)
            {
                TempData.Add("expandedTaxaInTree", model.ExpandedTaxa);
            }
            this.RootTaxonId = newRootTaxonId;

            if (model != null)
            {
                TaxonTreeViewItem treeViewItem = model.GetTreeViewItemByTaxonId(selectedTaxonId);
                if (treeViewItem != null)
                {
                    TempData.Add("selectedIdInTree", treeViewItem.Id);
                }
                else
                {
                    // this is the case when we have created a new taxon
                    TempData.Add("selectedTaxonIdInTree", selectedTaxonId);
                }
                //TempData.Add("selectedIdInTree", selectedTaxonId);
            }
        }
        /// <summary>
        /// Called from Dynatree as an ajax call.
        /// Returns the closest children to the taxonId as Json
        /// </summary>
        /// <param name="taxonId"></param>
        /// <returns></returns>
        public JsonNetResult GetTreeData(int taxonId)
        {
            var model = Session["TaxonTree"] as TaxonTreeViewModel;
            TaxonTreeViewItem item = model.GetTreeViewItemById(taxonId);

            if (item == null) // happens when the user navigates backwards and the tree is different from the one in session
            {
                ITaxon taxon = CoreData.TaxonManager.GetTaxon(GetCurrentUser(), taxonId);
                item = new TaxonTreeViewItem(taxon, null, 1, model.GetUniqueNumber(), true);

                //var str2 = Url.Content(@"~/Images/Icons/minus-small.png");
                //var url = new UrlHelper(this.Request.RequestContext);
                //var str = url.Content(@"~/Images/Icons/minus-small.png");
            }
            item.Children = model.LoadChildren(item, false);
            return(new JsonNetResult(item.Children));
        }