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?
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() });
}
}
I will use the IPublishedContent to set the one SelectedHikingDestination in table HikingDestinations
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; }
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?
Hi Tom.
Here is a basic example on how to do this with a SurfaceController and a PartialView.
SurfaceController: MyAwesomeController:
View: MyDropdown.cshtml:
And then to render the dropdown:
Best of luck to you!
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:
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](/media/upload/18b46b44-ea47-407f-8d00-bddc5cba7b27/HikingDestination01.jpg)
So I can use this to create a new record in the same way as rest of the fields that already works fine:
Extracted and simplified from my HikingDestinationViewModel: IEnumerable< SelectListItem > ListOfHikingDestinations
I got this to work now:
and loading the view with this
Thank you Dennis for what you showed me about Umbracohelper. It was really useful in this context.
is working on a reply...