I'm new in this razor world, but i'm trying do display some properties from members.
I have currently two properties:
A true/false property named "hideInMembersList" And a Upload property named "logo"
I managed to get the true/false property to work, but not the upload-field. Any one have to code to spare? I want there to be an if-sentence because not all members will have an image uploaded.
I guess it's super easy, but clearly doing something wrong. This i my currently working code:
How about using the type converters, something like this:
@{
var memberservice = ApplicationContext.Current.Services.MemberService;
}
@foreach (var member in memberservice.GetMembersByGroup("Members here"))
{
if (!member.GetValue<bool>("hideInMembersList"))
{
<p><strong><a href="[email protected]">@member.Name</a></strong></p>
}
if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
{
var logo = member.GetValue<string>("logo");
<img src="@logo" />
}
}
An alternative approach (if you don't need write access) might be to use the TypedMember helper, unfortunately there isn't a way to get members by group so we will still need the members service for this.
The positive to this is that the Members will be returned as IPublishedContent, the same as Content so you have HasValue etc...
e.g.
@{
var memberservice = ApplicationContext.Current.Services.MemberService;
}
@foreach (var member in memberservice.GetMembersByGroup("Members here"))
{
var typedMember = Umbraco.TypedMember(member.Id);
if (!typedMember.GetPropertyValue<bool>("hideInMembersList"))
{
<p><strong><a href="[email protected]">@typedMember.Name</a></strong></p>
}
if (typedMember.HasValue("logo"))
{
var logo = typedMember.GetPropertyValue<string>("logo");
<img src="@logo" />
}
}
Uttermost respect for that answer. Quick and perfectly working - both examples! Big thanks...
I guess, this makes my razor skills rather much in the dust - i have no idea how you came to those to snippets. And I'm not sure iI even get why both of them work - and whats the difference between the two?!
Could you maybe shortly elaborate before I mark an answer? Which is the best practice?
Just another follow up - the first code shows all the members with the "hideInMembersList" not checked (correct) The second code shows ONLY the members with the "hideInMembersList" checked (not correct)
Other than that - i have difficulty turning this into a list! You know <li></li>
I have rearranged and changed the code a bit, but why on earth does is make the "error loading..." because I add some HTML
Working
@foreach (var member in memberservice.GetMembersByGroup("FB Suppliers Member"))
{
if (!member.GetValue<bool>("hideInMembersList"))
{
<p><strong><a href="[email protected]">@member.Name</a></strong></p>
if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
{
var logo = member.GetValue<string>("logo");
<img src="@logo" />
}
}
}
Not working
<ul>
@foreach (var member in memberservice.GetMembersByGroup("FB Suppliers Member"))
{
<li>
if (!member.GetValue<bool>("hideInMembersList"))
{
<p><strong><a href="[email protected]">@member.Name</a></strong></p>
if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
{
var logo = member.GetValue<string>("logo");
<img src="@logo" />
}
}
</li>
}
</ul>
You're very welcome. I have fixed my second snippet above (I forgot the ! for the NOT operator).
So MemberService is a read/write API returning Member, it directly queries the database. TypedMember and most of the MemberShipHelper use IPublishedContent (read only) which is cached so doesn't use the database so is far quicker.
This should solve your html issue (just required an additional @ on the if to switch to code block):
<ul>
@foreach (var member in memberservice.GetMembersByGroup("FB Suppliers Member"))
{
<li>
@if (!member.GetValue<bool>("hideInMembersList")){
<p><strong><a href="[email protected]">@member.Name</a></strong></p>
if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
{
var logo = member.GetValue<string>("logo");
<img src="@logo" />
}
}
</li>
}
</ul>
Display properties from members
Hi guys,
I'm new in this razor world, but i'm trying do display some properties from members.
I have currently two properties:
A true/false property named "hideInMembersList"
And a Upload property named "logo"
I managed to get the true/false property to work, but not the upload-field. Any one have to code to spare?
I want there to be an if-sentence because not all members will have an image uploaded.
I guess it's super easy, but clearly doing something wrong. This i my currently working code:
It is a Partial View Macro File
I've tried the following:
Which is working on a Scripting File - but not here. It just gives the "error loading macro partial
Anyone who can help me out?
How about using the type converters, something like this:
An alternative approach (if you don't need write access) might be to use the TypedMember helper, unfortunately there isn't a way to get members by group so we will still need the members service for this.
The positive to this is that the Members will be returned as IPublishedContent, the same as Content so you have HasValue etc... e.g.
Uttermost respect for that answer. Quick and perfectly working - both examples! Big thanks...
I guess, this makes my razor skills rather much in the dust - i have no idea how you came to those to snippets.
And I'm not sure iI even get why both of them work - and whats the difference between the two?!
Could you maybe shortly elaborate before I mark an answer? Which is the best practice?
Again, a huge thank you!
Just another follow up - the first code shows all the members with the "hideInMembersList" not checked (correct)
The second code shows ONLY the members with the "hideInMembersList" checked (not correct)
Other than that - i have difficulty turning this into a list! You know <li></li>
I have rearranged and changed the code a bit, but why on earth does is make the "error loading..." because I add some HTML
Working
Not working
Thank you in advance!
You're very welcome. I have fixed my second snippet above (I forgot the ! for the NOT operator).
So MemberService is a read/write API returning Member, it directly queries the database. TypedMember and most of the MemberShipHelper use IPublishedContent (read only) which is cached so doesn't use the database so is far quicker.
This should solve your html issue (just required an additional @ on the if to switch to code block):
is working on a reply...