Help needed getting the url & title out of UrlPicker in an MVC Partial View?
Hello all,
I have a doc type "Widget" which is loaded into the page using a partial view. Inside here is the option to add a button to the widget which links to either an outside page or internal content. I've set all this up as a new data-type and chosen XML as the output however when I try to get the url and title for said new "button" I can't seem to find the right "hook" to get this info out?
Here's the code I have, please take a look and see where I'm making my mistake ... be honest, I can take it! ;-D
@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
@using uComponents.DataTypes.UrlPicker
@using uComponents.DataTypes.UrlPicker.Dto;
@{
Layout = null;
var nodeIds = Model.GetPropertyValue<string>("WidgetPanels").Split(',');
List<IPublishedContent> widgets = (from nodeId in nodeIds where !String.IsNullOrEmpty(nodeId) let publishedContent = Umbraco.NiceUrl(Convert.ToInt32(nodeId)) where !String.IsNullOrEmpty(publishedContent) && publishedContent != "#" select Umbraco.TypedContent(nodeId)).ToList();
}
@if( widgets.Any() )
{
foreach (var widget in widgets)
{
if (widget == null)
{
continue;
}
var widgetTitle = string.Empty;
string widgetButtonStyle;
var widgetBg = widget.GetPropertyValue("widgetHasBackground").ToString() == "True" ? "bg-white" : "";
if (!String.IsNullOrEmpty(widget.GetPropertyValue<string>("widgetTitle")))
{
widgetTitle = String.Format("<h4>{0}</h4>", widget.GetPropertyValue<string>("widgetTitle"));
}
var widgetContent = widget.GetPropertyValue<string>("widgetContent");
var widgetHasButton = widget.GetPropertyValue("widgetHasButton").ToString();
var widgetButtonText = widget.GetPropertyValue<string>("widgetButtonText");
switch (widget.GetPropertyValue("widgetButtonStyle").ToString())
{
case "26":
widgetButtonStyle = "btn-blue";
break;
case "27":
widgetButtonStyle = "btn-red";
break;
default:
widgetButtonStyle = "btn-gray";
break;
}
@*HERE WE GO*@
UrlPickerState links = null;
var widgetButtonAction = widget.GetPropertyValue("widgetButtonAction");
if (widgetButtonAction != null)
{
links = UrlPickerState.Deserialize( widgetButtonAction.ToString() );
};
var sectionClass = @widgetTitle.Contains("Wellbeing Portal") ? "pad-10 rounded-10 box-shadow-black-30 gradient-off_white" : "";
<div class="sidebarwidget @widgetBg">
<div class="@sectionClass">
@Html.Raw(widgetTitle)
@Html.Raw(widgetContent)
@if (@widgetHasButton == "True")
{
if (links != null)
{
switch ((UrlPickerMode) links.Mode)
{
case UrlPickerMode.Content:
case UrlPickerMode.URL:
<a class="button @widgetButtonStyle rounded-5 action" href="@links.Url" title="@(string.IsNullOrWhiteSpace(links.Title) ? widgetButtonText : links.Title)">@widgetButtonText</a>
break;
}
}
@*
simple version which returns DynamicXML for the url:
<a class="button @widgetButtonStyle rounded-5 action" href="@widgetButtonAction">@widgetButtonText</a>
*@
}
</div>
</div>
}
}
Finally, if there's any "better" way you think I could do any of this then please feel free to make comments about my code, I'm always open to learning!
I haven't used the new DynamicXml object (yet) ... but I know that if you use `ToString()` it would output the full Type name(space). Which is no go for the `UrlPickerState.Deserialize` call.
Taking a quick look at the core code (only because I have it to hand), I see there is a `ToXml()` method... which might work?
UrlPickerState links = null;
var widgetButtonAction = widget.GetPropertyValue("widgetButtonAction").Value;
if (widgetButtonAction != null)
{
links = UrlPickerState.Deserialize( widgetButtonAction.ToXml() );
};
Yes, there seems to be subtle differences between `GetPropertyValue` and `GetProperty(Model).Value`. I think the `GetPropertyValue` gets the string value, whereas the `GetProperty` runs through the `IPropertyEditorValueConverter` code, (for strongly-typed objects).
In response, and to help anyone else, the "ToXML" method doesn't show in the VS intellisense and I've checked it against the original code which then breaks the code even further:
CS1928: 'object' does not contain a definition for 'ToXml' and the best extension method overload 'umbraco.NodeExtensions.ToXml(umbraco.NodeFactory.Node)' has some invalid arguments
So I think I'll go with my solution unless anyone else shows us a better way!
I also wanted to say, thank you for the reference! Getting to these properties via UrlPickerState.Deserialize(myTypedNode.GetProperty("myLinkPickerProp").Value.ToString()).Url etc. isn't the easiest thing to remember or type out, but does seem to be the shortest route. (Of course, you'll probably want to check for null like Jon did, this was only me regurgitating for posterity!)
Help needed getting the url & title out of UrlPicker in an MVC Partial View?
Hello all,
I have a doc type "Widget" which is loaded into the page using a partial view. Inside here is the option to add a button to the widget which links to either an outside page or internal content. I've set all this up as a new data-type and chosen XML as the output however when I try to get the url and title for said new "button" I can't seem to find the right "hook" to get this info out?
Here's the code I have, please take a look and see where I'm making my mistake ... be honest, I can take it! ;-D
Thank you and HAPPY 2013!!!
Hi Jon,
Do you know if the `widgetButtonAction` variable has a value before it is passed to the `UrlPickerState.Deserialize` call?
I'm wondering if anything happens to the value that might cause the deserialisation to fail. Hmmm.
Thanks, Lee.
Lee,
When I try to initialise as a string as follows:
this returns:
Umbraco.Core.Dynamics.DynamicXml
Does this help?
J
I haven't used the new DynamicXml object (yet) ... but I know that if you use `ToString()` it would output the full Type name(space). Which is no go for the `UrlPickerState.Deserialize` call.
Taking a quick look at the core code (only because I have it to hand), I see there is a `ToXml()` method... which might work?
Cheers, Lee.
Lee, et al:
I continued with messing about with this and got it to work, but it seems to be a bit of a hack, not sure why this wouldn't be the same as before?
This is the new snippet that works:
But this is only different in the way the value of widgetButtonAction is retrieved: GetPropertyValue vs. GetProperty(Model).Value?
Thoughts?
Yes, there seems to be subtle differences between `GetPropertyValue` and `GetProperty(Model).Value`. I think the `GetPropertyValue` gets the string value, whereas the `GetProperty` runs through the `IPropertyEditorValueConverter` code, (for strongly-typed objects).
Lee,
In response, and to help anyone else, the "ToXML" method doesn't show in the VS intellisense and I've checked it against the original code which then breaks the code even further:
CS1928: 'object' does not contain a definition for 'ToXml' and the best extension method overload 'umbraco.NodeExtensions.ToXml(umbraco.NodeFactory.Node)' has some invalid arguments
So I think I'll go with my solution unless anyone else shows us a better way!
Final version:
Thanks for all the help!
Jon
Don't forget to add
to the top of your partial view!
I also wanted to say, thank you for the reference! Getting to these properties via
UrlPickerState.Deserialize(myTypedNode.GetProperty("myLinkPickerProp").Value.ToString()).Url
etc. isn't the easiest thing to remember or type out, but does seem to be the shortest route. (Of course, you'll probably want to check for null like Jon did, this was only me regurgitating for posterity!)Thanks again!
For who encounters the same issue, hope this helps.
In my case, at the top of my rator view I have:
This works for me without any deserialize. I use "XML" for the UrlPicker, tested on umbraco 6.2.0.
But I don't use the code above in my view. I use a helper method to generate the HTML link, as somebody in a previous post shared:
So in the view, to get the linkHTML and render it in the view:
is working on a reply...