I struggled with this for a while, having not worked in razor or with the Dropdown data type much. I couldn't find very good documentation on it. So, regardless of whether my use of this technique is the best approach to the problem or anything like that, the explanation may help someone else.
So the problem is:
You have a media type which includes a DropDown data type. I am using it to keep a list of access levels so various media can be loaded into the page if they have access to it or a prompt to where they can get it if not. So I need to read what this media type is and what level of access has been selected to be associated with it. In this case it's a list of digital resources associated with a book.
The problem is, in razor you get the Id and not the Value. To get the value you need to match the values in the DropDown datatype to the Id you have from selecting the item.
// Runs over a media folder of these Media Items
var media = @Model.MediaById("1061"); var items = media.Children; foreach(var item in items) { if (!String.IsNullOrEmpty(item.title) && !String.IsNullOrEmpty(@item.courseAccessPicker)) { // GetPreValues takes the DataType ID, which you find by mousing over the data type in Umbraco and looking at the bottom left corner of the window XPathNodeIterator iterator = umbraco.library.GetPreValues(-39); iterator.MoveNext(); XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", ""); // Ensure the value cannot be empty incase nothing matches string preValue = "something";
while (preValues.MoveNext()) { // Compare the id of the nodes against what the media access is if( preValues.Current.GetAttribute("id", "") == @item.courseAccessPicker ){ preValue = preValues.Current.Value; } } if (Roles.IsUserInRole(@preValue)){ <div class="col4"> <article class="book"> <p><a href="@item.coursePage" class="book-button open">Open</a></p> <figure class=""> <a href="@item.coursePage"> <img src="@Model.MediaById(@item.thumbnail).umbracoFile" alt="@item.title" /> </a> </figure> <header> <h1><a href="@item.coursePage">@item.title</a></h1> </header> </article> </div> } else { <div>Add whatever you want here as the alternative</div> } } }
Retrieving Selected Items from Dropdown datatype
Hello,
I struggled with this for a while, having not worked in razor or with the Dropdown data type much. I couldn't find very good documentation on it. So, regardless of whether my use of this technique is the best approach to the problem or anything like that, the explanation may help someone else.
So the problem is:
You have a media type which includes a DropDown data type. I am using it to keep a list of access levels so various media can be loaded into the page if they have access to it or a prompt to where they can get it if not. So I need to read what this media type is and what level of access has been selected to be associated with it. In this case it's a list of digital resources associated with a book.
The problem is, in razor you get the Id and not the Value. To get the value you need to match the values in the DropDown datatype to the Id you have from selecting the item.
// Runs over a media folder of these Media Items
var media = @Model.MediaById("1061");
var items = media.Children;
foreach(var item in items)
{
if (!String.IsNullOrEmpty(item.title) && !String.IsNullOrEmpty(@item.courseAccessPicker))
{
// GetPreValues takes the DataType ID, which you find by mousing over the data type in Umbraco and looking at the bottom left corner of the window
XPathNodeIterator iterator = umbraco.library.GetPreValues(-39);
iterator.MoveNext();
XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
// Ensure the value cannot be empty incase nothing matches
string preValue = "something";
while (preValues.MoveNext())
{
// Compare the id of the nodes against what the media access is
if( preValues.Current.GetAttribute("id", "") == @item.courseAccessPicker ){
preValue = preValues.Current.Value;
}
}
if (Roles.IsUserInRole(@preValue)){
<div class="col4">
<article class="book">
<p><a href="@item.coursePage" class="book-button open">Open</a></p>
<figure class="">
<a href="@item.coursePage">
<img src="@Model.MediaById(@item.thumbnail).umbracoFile" alt="@item.title" />
</a>
</figure>
<header>
<h1><a href="@item.coursePage">@item.title</a></h1>
</header>
</article>
</div>
}
else
{
<div>Add whatever you want here as the alternative</div>
}
}
}
Hope that helps anyone who was in my situation.
is working on a reply...