Binding dropdown using macro

joyanta sen asked on October 4, 2018 01:30

Hi, I have a field in a Page type which is a dropdown list control. I need to bind the dropdown with a List which is returned from a macro. And the method in the macro is returning the following list.

For ex:

return TopicsInfoProvider.GetTopics().Columns("TopicsID", "TopicName");

But the above is not working. Could you please let me know how to bind the drop down with this list using Macro expression.

Thanks.

Recent Answers


Peter Mogilnitski answered on October 4, 2018 03:20 (last edited on December 10, 2019 02:31)

It expects IEnumerable. Helps says: Macro expression (without macro brackets). The result of the expression should be an enumerable object whose items will be used to fill the list. For example: CurrentUser.PurchasedProducts.DisplayNames.

Try: TopicsInfoProvider.GetTopics().Columns("TopicsID", "TopicName").AsEnumerable() or TopicsInfoProvider.GetTopics().Columns("TopicName").AsEnumerable()

Dont forget to import linq namespace.

If this doesnt work you may switch to List of Options and try to macro directly there:

{%TopicsInfoProvider.GetTopics().Columns("TopicsID", "TopicName").AsEnumerable()
.Select(t => t.TopicID.ToString() + ";" + t.TopicName + "\n" )|(identity)GlobalAdministrator%} and it works.

P.S. Is there a reason why you dont want to simply query your Topic table?

0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on October 4, 2018 16:40

HI, Thanks for your response.

TopicsInfoProvider.GetTopics().Columns("TopicsID", "TopicName").AsEnumerable() or TopicsInfoProvider.GetTopics().Columns("TopicName").AsEnumerable()

Both are not working. It is displaying TopicName along with the Object name. Like Material.TopicInfo(material.topics)-"Name of the Topic"

Am I missing something?

I am not querying directly, since I need to display only those topics which are part of some other object or table and in that table topic ids are present as comma separated.

Thanks.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 4, 2018 18:41 (last edited on December 10, 2019 02:31)

I guess this is how ToString works. Did you try the second suggestion?

Remove Columns, i.e. your macro should do just return TopicsInfoProvider.GetTopics().AsEnumerable()

Switch Data source to List of Options and try 2nd macro directly there:

{% TopicsMacro.Transform("{#TopicsID#};{#TopicName#}\n")|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.