November Happy Hour will be moved to Thursday December 5th.

Save and display PageReference/ContentReference in a list?

Vote:
 

Console.WriteLine(" Hello Everybody");

I'am trying to build a customizable top-navigation-bar (in EpiServer 7.5, MVC).

I made the site MainNavigation in the Left-nav.

What I want to do is make it so that every logedin site-user can customize the top-nav-meny by adding pages from the MainNav (clicking a button or something like that) and save the ContentLink and/or PageRefercenes in the EpiServerProfile class so it can be displayed in the custom top-nav as links.

But i'am a bit stuck, cause i cant find a good way to save the pages (with the EpiserverProfile class) display the pages.

Anybody got some wisdom and wanna earn some sweet code-karma it would me much appreciated :)

/Stefan from the North

#109847
Oct 16, 2014 8:55
Vote:
 

I managed to do this, but I'm not sure if it's the best way, but it works:

Controller:

        public ActionResult Index(CustomNavPage currentPage)
        {
            CustomNavPageViewModel model = new CustomNavPageViewModel();
            if ((CustomNavPageViewModel)Session["model"] != null)
            {
                model = (CustomNavPageViewModel)Session["model"];
            }
            else
            {
                model.AvailablePageList = GetAvailablePages();
                model.SelectedPageList = new List<PageData>();
            }
            Session["model"] = model;

            return View(model);
        }

        public ActionResult SetCustomLinks(int pageID)
        {
            var model = (CustomNavPageViewModel)Session["model"];
            Session.Clear();
            var selectedPage = new PageData();
            if (model.SelectedPageList == null || model.SelectedPageList.Count == 0)
            {
                model.SelectedPageList = new List<PageData>();
            }
            if (model.AvailablePageList != null || model.AvailablePageList.Count != 0)
            {
                foreach (var page in model.AvailablePageList)
                {
                    if (page.ContentLink.ID == pageID)
                    {
                        model.SelectedPageList.Add(page);
                        selectedPage = page;
                    }
                    else 
                    {
                        continue;
                    }
                }
                model.AvailablePageList.Remove(selectedPage);
            }

            Session["model"] = model;

            return View("Index", model);
        }

        public ActionResult RemoveCustomLinks(int pageID)
        {
            var model = (CustomNavPageViewModel)Session["model"];
            Session.Clear();
            var selectedPage = new PageData();
            if (model.AvailablePageList == null || model.AvailablePageList.Count == 0)
            {
                model.AvailablePageList = new List<PageData>();
            }
            if (model.SelectedPageList != null || model.SelectedPageList.Count != 0)
            {
                foreach (var page in model.SelectedPageList)
                {
                    if (page.ContentLink.ID == pageID)
                    {
                        model.AvailablePageList.Add(page);
                    }
                }
                model.SelectedPageList.Remove(selectedPage);
            }

            Session["model"] = model;

            return View("Index", model);
        }

        public List<PageData> GetAvailablePages()
        {
            var availablePageList = new List<PageData>();
            var pageList = DataFactory.Instance.GetChildren(PageReference.StartPage);

            foreach (var page in pageList)
            {
                availablePageList.Add(page);
            }

            return availablePageList;
        }

ViewModel:

    public class CustomNavPageViewModel
    {
        public List<PageData> AvailablePageList { get; set; }
        public List<PageData> SelectedPageList { get; set; }
    }

View:

@model BlocketProject.Models.ViewModels.CustomNavPageViewModel
@{
    ViewBag.Title = "Index";
}

@if (Model.AvailablePageList.Count != 0 && Model.AvailablePageList != null)
{
    <h2>Tillgängliga länkar:</h2>

    foreach (var page in Model.AvailablePageList)
    {
        using (Html.BeginForm("SetCustomLinks", "CustomNavPage", new {@pageID = page.ContentLink.ID }, FormMethod.Post))
        {
            <span><button type="submit" class="linkButton">+</button> @page.Name</span>
        }
    }
}

@if (Model.SelectedPageList.Count != 0 && Model.SelectedPageList != null)
{
    <h2>Valda länkar:</h2>

    foreach (var page in Model.SelectedPageList)
    {
        using (Html.BeginForm("RemoveCustomLinks", "CustomNavPage", new { @pageID = page.ContentLink.ID }, FormMethod.Post))
        {
            <span><button type="submit" class="linkButton">-</button> @page.Name</span>
        }
    }

    <div>
 
        <ul class="YourMenu">
            @foreach (var page in Model.SelectedPageList)
            {
                <li>
                    <a href="@page.LinkURL">@page.Name</a>
                </li>
            }
        </ul>
    </div>
}


I hope this works for you buddy! ;)

#112067
Oct 22, 2014 12:17
Vote:
 

Forgot to add a line, replace the RemoveCustomLinks with the following to make it work correctly:

        public ActionResult RemoveCustomLinks(int pageID)
        {
            var model = (CustomNavPageViewModel)Session["model"];
            Session.Clear();
            var selectedPage = new PageData();
            if (model.AvailablePageList == null || model.AvailablePageList.Count == 0)
            {
                model.AvailablePageList = new List<PageData>();
            }
            if (model.SelectedPageList != null || model.SelectedPageList.Count != 0)
            {
                foreach (var page in model.SelectedPageList)
                {
                    if (page.ContentLink.ID == pageID)
                    {
                        model.AvailablePageList.Add(page);
                        selectedPage = page;
                    }
                }
                model.SelectedPageList.Remove(selectedPage);
            }

            Session["model"] = model;

            return View("Index", model);
        }
#112072
Oct 22, 2014 13:12
Vote:
 
#112074
Oct 22, 2014 13:42
Vote:
 

Very Nice Thanx Ludvig :)

#112075
Oct 22, 2014 13:44
Vote:
 

Very Nice Thanx Ludvig :)

#112076
Oct 22, 2014 13:44
Vote:
 

Also, replace:

<ul class="YourMenu">
        @foreach (var page in Model.SelectedPageList)
         {
            <li>
                <a href="@page.LinkURL">@page.Name</a>
            </li>
         }
</ul>

with:

<ul class="YourMenu">
        @foreach (var page in Model.SelectedPageList)
         {
            <li>
                @Html.UrlLink(page.LinkURL, page.Name)
            </li>
         }
</ul>

for a nicer URL in the browser :)

#112084
Oct 22, 2014 14:29
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.