Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with getting value from a Macro partial #90

Open
MMasey opened this issue Feb 11, 2019 · 5 comments
Open

Issue with getting value from a Macro partial #90

MMasey opened this issue Feb 11, 2019 · 5 comments

Comments

@MMasey
Copy link

MMasey commented Feb 11, 2019

Heyo,

I'm currently have some issues with getting the IEnumerable<Link> value whilst inside a Macro Partial. I think it's because in order to retrieve a value from a Macro, you need to use Model.GetParameterValue<IEnumerable<Link>>("links"); (for example) which i'm guessing means it doesn't use the standard property value converters.

I'm able to get the value as a string, but i'm not sure what the best way is to convert that into the IEnumerable<Link> value i need.

Do you have any suggestions?
Thanks

P.S. super excited to see that this is going into core, H5YR!

@rasmusjp
Copy link
Owner

rasmusjp commented Feb 11, 2019

Hi @MMasey

I've actually never tried using it in a macro, but having a look at the Core source, it does look like property value converters are not getting called (which kinda makes sense since it isn't a property).

I guess one way to do it, is to create an extension method that does the conversion

using System.Collections.Generic;
using System.Linq;
using System.Web;
using RJP.MultiUrlPicker.Models;
using Umbraco.Web.Models;

namespace WebApplication
{
    public static class PartialViewMacroModelExtensions
    {
        public static IEnumerable<Link> GetLinks(this PartialViewMacroModel model, string alias)
        {
            var value = model.GetParameterValue<string>(alias);
            if(value == null)
            {
                return Enumerable.Empty<Link>();
            }

            return new MultiUrls(HttpUtility.HtmlDecode(value));
        }

        public static Link GetLink(this PartialViewMacroModel model, string alias)
        {
            return GetLinks(model, alias).FirstOrDefault();
        }
    }
}

and then you can use it in your macro partial

@foreach(var link in Model.GetLinks("test"))
{
    <a href="@link.Url">@link.Name</a>
}

@MMasey
Copy link
Author

MMasey commented Feb 12, 2019

It works like a charm, thank you so much 😄

I have a question regarding the MultiUrls class. In your source it says that it is obsolete and you should used IEnumberable<Link> instead, but then you are using it in the property value converter. Does this mean that with future versions this may no longer work?

Also, if this is going into core, this information would be useful in the docs (I know it's a very small use case, but you never know)

@rasmusjp
Copy link
Owner

Happy to hear that it works 🙂

The plan was to remove the MultiUrls class in the next major release, but since the editor is now part of Core I will not be updating this package anymore. So you can safely use the class.

I noticed that it was marked as a macro parameter editor in Core too and after talking to @nul800sebastiaan I created a PR to remove it since it's not supported because macro parameters don't use the value converters.

@MMasey
Copy link
Author

MMasey commented Feb 12, 2019

Nice one, thanks for the update 👍

@cryothic
Copy link

Happy to hear that it works

The plan was to remove the MultiUrls class in the next major release, but since the editor is now part of Core I will not be updating this package anymore. So you can safely use the class.

I noticed that it was marked as a macro parameter editor in Core too and after talking to @nul800sebastiaan I created a PR to remove it since it's not supported because macro parameters don't use the value converters.

Does that mean they will remove the macro parameter editor for new releases of Umbraco?

btw, thanks for the comment, since I was getting the same issue.
Although I solved it like this in my macro:
var LinkList = new MultiUrls(HttpUtility.HtmlDecode(Model.MacroParameters["fldLinks"].ToString()));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants