I am using UMbraco Members so people can log into my umbraco site. They can update their profiles, and upload an image as their logo. This uses the Media Service API to create a media item and upload the image. That all works fine.
I have a page that esentially lists all members of the site and displays their logos. To do this, I am using a Razor Macro. Now in my code, I am checking to see if the member has the the property Logo, then checking to see if the length is greater than 0. If so, then i generate a media variable by Model.MediaById and diplay the umbracoFile using imageGen. If they member does not have a logo, or length is not greater than 0 then i swap it for an avatar.
My code is breaking for some reaons as soon as i call @selectedMedia.umbracoFile for a member that has never had a logo uploaded. My code works fine every other time. I need need to figure out what is wrong with the code. So any ideas would be great please.
Media issue in Razor
Hi
I am using UMbraco Members so people can log into my umbraco site. They can update their profiles, and upload an image as their logo. This uses the Media Service API to create a media item and upload the image. That all works fine.
I have a page that esentially lists all members of the site and displays their logos. To do this, I am using a Razor Macro. Now in my code, I am checking to see if the member has the the property Logo, then checking to see if the length is greater than 0. If so, then i generate a media variable by Model.MediaById and diplay the umbracoFile using imageGen. If they member does not have a logo, or length is not greater than 0 then i swap it for an avatar.
My code is breaking for some reaons as soon as i call @selectedMedia.umbracoFile for a member that has never had a logo uploaded. My code works fine every other time. I need need to figure out what is wrong with the code. So any ideas would be great please.
Here is my code:
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.cms.businesslogic.member
@foreach(var item in Member.GetAll){
<div class="item">
<ul>
<li><h3>@item.Text</h3></li>
@if (@item.HasProperty("logo"))
{
if(@item.getProperty("logo").Value.ToString().Length > 0)
{
var selectedMedia = Model.MediaById(@item.getProperty("logo").Value.ToString());
<li><figure><a href="/brokers/view-broker/[email protected]"><img src="/[email protected]" alt="@item.Text" title="@item.Text" /></a></figure></li>
}
}
else
{
<li><figure><a href="/brokers/view-broker/[email protected]"><img src="/imageGen.ashx?image=/images/avatar.png" alt="@item.Text" title="@item.Text" /></a></figure></li>
}
<li><b>Phone:</b> @item.getProperty("phone").Value.ToString();</li>
<li><b>Fax:</b> @item.getProperty("fax").Value.ToString()</li>
<li><b>Website:</b> @item.getProperty("website").Value.ToString();</li> <li><b>Email:</b> <a href="mailto:@item.Email">@item.Email</a></li>
<li><a href="/brokers/view-broker/[email protected]" class="button orange">Details</a></li>
</ul>
</div><!--/ .item-->
}
Hi Hugh,
Try doing this to check if Logo has a value.
Hope this helps
Hi Fuji
Thanks for this. However; I get an error when trying to save my Macro "error CS0103: The name 'memLogo' does not exist in the current context"
Here is my code:
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.cms.businesslogic.member
@{
var memberId = HttpContext.Current.Request.QueryString["id"];
var cInt = 0;
}
@foreach(var item in Member.GetAll){
cInt = cInt + 1;
if (cInt % 3 == 0)
{
<div class="item last">
<ul>
<li><h3>@item.Text</h3></li>
var memLogo = "";
memLogo = @item.getProperty("logo").Value.ToString();
@if(@memLogo !=""){
var selectedMedia = Model.MediaById(@memLogo);
var logoItem = @memLogo.umbracoFile;
<li><figure><a href="/brokers/view-broker/[email protected]"><img src="/imageGen.ashx?@logoItem&width=150&height=191" alt="@item.Text" title="@item.Text" /></a></figure></li>
}
else{
<li><figure><a href="/brokers/view-broker/[email protected]"><img src="/imageGen.ashx?image=/images/avatar.png" alt="@item.Text" title="@item.Text" /></a></figure></li>
}
<li><b>Phone:</b> @item.getProperty("phone").Value.ToString();</li>
<li><b>Fax:</b> @item.getProperty("fax").Value.ToString()</li>
<li><b>Website:</b> @item.getProperty("website").Value.ToString();</li>
<li><b>Email:</b> <a href="mailto:@item.Email">@item.Email</a></li>
<li><a href="/brokers/view-broker/[email protected]" class="button orange">Details</a></li>
</ul>
</div><!--/ .item-->
<div style="clear:both;"></div>
}
else
{
<div class="item">
<ul>
<li><h3>@item.Text</h3></li>
var memLogo = "";
memLogo = @item.getProperty("logo").Value.ToString();
@if(memLogo !=""){
var selectedMedia = Model.MediaById(@memLogo);
var logoItem = @memLogo.umbracoFile;
<li><figure><a href="/brokers/view-broker/[email protected]"><img src="/imageGen.ashx?@logoItem&width=150&height=191" alt="@item.Text" title="@item.Text" /></a></figure></li>
}
else{
<li><figure><a href="/brokers/view-broker/[email protected]"><img src="/imageGen.ashx?image=/images/avatar.png" alt="@item.Text" title="@item.Text" /></a></figure></li>
}
<li><b>Phone:</b> @item.getProperty("phone").Value.ToString();</li>
<li><b>Fax:</b> @item.getProperty("fax").Value.ToString();</li>
<li><b>Website:</b> @item.getProperty("website").Value.ToString();</li>
<li><b>Email:</b> <a href="mailto:@item.Email">@item.Email</a></li>
<li><a href="/brokers/view-broker/[email protected]" class="button orange">Details</a></li>
</ul>
</div><!--/ .item-->
}
}
Try this instead but
hope this helps but you need only one else
//fuji
is working on a reply...