Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Engan 430 posts 1173 karma points
    Jun 21, 2017 @ 15:59
    Tom Engan
    0

    Looping through IPublishedContent, into a dropdownlist

    I have a website I want to loop through a set of published, preselected pages (maybe filtered by crossing out a new property in document type), and implement the id as value, and name (or url) as text into a dropdownlist.

    Someone who has a function / helper I can use in a surfacecontroller for this task?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jun 22, 2017 @ 06:32
    Dennis Adolfi
    1

    Hi Tom.

    Here is a basic example on how to do this with a SurfaceController and a PartialView.

    SurfaceController: MyAwesomeController:

    public class MyAwesomeController : SurfaceController
        {
            public UmbracoHelper UmbracoHelper;
    
            public MyAwesomeController()
            {
                this.UmbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            }
    
            public ActionResult MyDropdown()
            {
                var pages = GetPages();
                return View("/Views/Partials/MyDropdown.cshtml", MapAsViewModel(pages));
            }
    
    
            public IEnumerable<IPublishedContent> GetPages()
            {
                // This is just an example, replace code in here with you own magic code for gathering the content.
                return UmbracoHelper.AssignedContentItem.Descendants();
            }
    
            public IEnumerable<SelectListItem> MapAsViewModel(IEnumerable<IPublishedContent> content)
            {
                return content.Select(item => new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
            }
        }
    

    View: MyDropdown.cshtml:

    @model IEnumerable<SelectListItem>
    @{
        Layout = null;   
    }
    @Html.DropDownList("MyDropdown", Model)
    

    And then to render the dropdown:

    @Html.Action("MyDropdown", "MyAwesome")
    

    Best of luck to you!

  • Tom Engan 430 posts 1173 karma points
    Jun 22, 2017 @ 09:03
    Tom Engan
    1

    Thanks, I'll try to use some of this, but do you know how this can be used like this example used in this helpermethod?

    The only difference here is that this (helper)method populate data types instead of IPublishedContent.

    Most important to pay attention for:

    public List<SelectListItem> GetPreValuesFromDataTypeId(int dataTypeId)
    {
        List<SelectListItem> preValueSelectorList = new List<SelectListItem>();
    
        XPathNodeIterator iterator = umbraco.library.GetPreValues(dataTypeId);
        iterator.MoveNext();
        XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
    
        while (preValues.MoveNext())
        {
            string preValueIdAsString = preValues.Current.GetAttribute("id", "");
            int preValueId = 0;
            int.TryParse(preValueIdAsString, out preValueId);
            string preValue = preValues.Current.Value;
            preValueSelectorList.Add(new SelectListItem { Value = preValueIdAsString, Text = preValue }); 
        }
    
        return preValueSelectorList;
    }
    

    From this example here (works fine in my case, but now I want to populate published pages in dropdown instead of data types):

    http://www.codeshare.co.uk/blog/how-to-use-an-umbraco-data-type-to-populate-an-mvc-drop-down-list/


    I will use the IPublishedContent to set the one SelectedHikingDestination in table HikingDestinations enter image description here

    So I can use this to create a new record in the same way as rest of the fields that already works fine:

    [HttpPost]
    public ActionResult CreateHikingDestination(HikingDestinationViewModel model)
    {
        IMember member = Services.MemberService.GetByEmail(this.Members.CurrentUserName);
    
        var HikingDestinationAdd = new HikingDestinationViewModel();
    
        //member.id has the same value as field nodeId in db table cmsMember
        HikingDestinationAdd.nodeId = member.Id; 
        HikingDestinationAdd.SelectedHikingDestination = model.SelectedHikingDestination;
        HikingDestinationAdd.Title = model.Title;
        HikingDestinationAdd.StartDate = model.StartDate.Date;
        HikingDestinationAdd.HikingCode = model.HikingCode;
    
        var db = ApplicationContext.DatabaseContext.Database;
        db.Insert(HikingDestinationAdd);
        return RedirectToCurrentUmbracoPage();
    }
    

    Extracted and simplified from my HikingDestinationViewModel: IEnumerable< SelectListItem > ListOfHikingDestinations

    public int nodeId { get; set; }
    public IEnumerable<SelectListItem> ListOfHikingDestinations { get; set; }
    public string SelectedHikingDestination { get; set; }
    public string Title { get; set; }
    public DateTime StartDate { get; set; }
    public string HikingCode { get; set; }
    
  • Tom Engan 430 posts 1173 karma points
    Jun 22, 2017 @ 12:57
    Tom Engan
    100

    I got this to work now:

    public List<SelectListItem> ListHikingDestinations(int NodeId)
    {
        List<SelectListItem> preValueSelectorList = new List<SelectListItem>();
    
        IEnumerable<IPublishedContent> items = UmbracoHelper.Content(NodeId).Children;
        //Some logic with items here..
    
        foreach (IPublishedContent item in items)
        {
            preValueSelectorList.Add(new SelectListItem { Value = item.Id.ToString(), Text = item.Name });
        }
        return preValueSelectorList;
    }
    

    and loading the view with this

    public UmbracoHelper UmbracoHelper;
    
    [ChildActionOnly]
    public ActionResult ReadHikingDestination()
    {
        HikingDestinationViewModel model = new HikingDestinationViewModel();
    
        this.UmbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        model.ListOfHikingDestinations = ListHikingDestinations(1122);
        model.StartDate = System.DateTime.Now;
    
        return PartialView("CreateHikingDestination", model);
    }
    

    Thank you Dennis for what you showed me about Umbracohelper. It was really useful in this context.

Please Sign in or register to post replies

Write your reply to:

Draft