Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hugh 30 posts 64 karma points
    Feb 07, 2014 @ 08:45
    Hugh
    0

    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>&nbsp;@item.getProperty("phone").Value.ToString();</li>
    <li><b>Fax:</b>&nbsp;@item.getProperty("fax").Value.ToString()</li>
    <li><b>Website:</b>&nbsp;@item.getProperty("website").Value.ToString();</li> <li><b>Email:</b>&nbsp;<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-->
     }

  • Fuji Kusaka 2203 posts 4220 karma points
    Feb 07, 2014 @ 12:13
    Fuji Kusaka
    0

    Hi Hugh,

    Try doing this to check if Logo has a value.

    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>

    Hope this helps

  • Hugh 30 posts 64 karma points
    Feb 16, 2014 @ 03:08
    Hugh
    0

    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>&nbsp;@item.getProperty("phone").Value.ToString();</li>

    <li><b>Fax:</b>&nbsp;@item.getProperty("fax").Value.ToString()</li>

    <li><b>Website:</b>&nbsp;@item.getProperty("website").Value.ToString();</li>

    <li><b>Email:</b>&nbsp;<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>&nbsp;@item.getProperty("phone").Value.ToString();</li>

    <li><b>Fax:</b>&nbsp;@item.getProperty("fax").Value.ToString();</li>

    <li><b>Website:</b>&nbsp;@item.getProperty("website").Value.ToString();</li>

    <li><b>Email:</b>&nbsp;<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-->

    }

     }

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Feb 17, 2014 @ 18:15
    Fuji Kusaka
    0

    Try this instead but 

    @foreach(var item in Member.GetAll){
    
            cInt = cInt + 1;
            var memLogo = "";
            memLogo = @item.getProperty("logo").Value.ToString();
    
                if (cInt % 3 == 0){
    //do something
    @memLogo
    }
    else{
    //do something else
    }

    hope this helps but you need only one else

    //fuji

Please Sign in or register to post replies

Write your reply to:

Draft