rasmusjp / umbraco-multi-url-picker

Multi Url Picker for Umbraco 7
MIT License
31 stars 29 forks source link

New data types with max number of items set to 1 will return a single Link or null #59

Closed ghost closed 6 years ago

ghost commented 7 years ago

In the changelog, i read "New data types with max number of items set to 1 will return a single Link or null. Existing data types will continue to return IEnumerable."

How can I use the "New data type", instead of the "Existing data type"? I see no difference in the DataType property editor naming (this is Multi Url Picker). Or should I read Existing vs New as when document types already have a Multi Url Picker property defined vs when one is newly added?

rasmusjp commented 7 years ago

I can see the wording is a little confusing, existing data type would be if you have created a Multi Url Picker datatype in pre v2 and upgraded.

So if you create a new datatype (with v2+ installed) and select the Multi Url Picker, it should use Link/null if max items is set to 1.

ghost commented 7 years ago

Ah, thanks for clarifying. So if I'd like to upgrade the codebase, I would need to remove the datatype and recreate that? Which would inevitable lead to the loss of data? (No offence, just trying to oversee the consequences).

rasmusjp commented 7 years ago

Sure, no problem. It's a situation I haven't thought about.

I can think of a couple of solutions that might work if you upgrade from pre v2 and want to use the new format and not lose your data.

Another solution would be to implement a way to define that you want to use the new format, unfortunately I don't have time to look into this right now.

ngquoctrong commented 7 years ago

Hi Rasmusjp, I use Umbraco 7.7.2. When i set Max number of item is 1, I cannot get this link. It alway null. MultiUrls singleLink = CurrentPage.GetPropertyValue<MultiUrls>("singleLink"); if(singleLink != null){ <a href="@singleLink.Url">hello</a> } I got a error message

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for 'System.Tuple.Create<object,int>(object, int)' has some invalid arguments

graphicelements commented 7 years ago

Hi @rasmusjp, I get the same error as @ngquoctrong. On Umbraco 7.6.6.

..and if try to run this code, it dosent throw me anything. @if(item.HasValue("buttonLink")) { MultiUrls link = item.GetPropertyValue<MultiUrls>("buttonLink"); <a href="@link">@link</a> }

rasmusjp commented 7 years ago

If max number is 1, the returned value is Link otherwise it will be IEnumerable<Link>.

So if you're not using the ModelsBuilder (which you totally should), it should be something like this to get a single link

@{ var link = Model.Content.GetPropertyValue<Link>("myLink"); }

@if(link != null)
{
  <a href="@link.Url" target="@link.Target">@link.Name</a>
}
rasmusjp commented 6 years ago

I guess you've figured this out?