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
    Jan 20, 2014 @ 06:07
    Hugh
    0

    Razor Boolean Issue

    I have a field called isFeatured which is a true/false boolean field.  I am using this is in a Partial View Macro.

    I am trying to us this in a if statement but can not seem to get the boolean value out:3

    if (@p.GetProperty<bool>("isFeatured").Value)
    {
    do something
    }

    if (@p.GetProperty<bool>("isFeatured"))
    {
    do something
    }

    if (@p.GetProperty("isFeatured"))
    {
    do something
    }

    All the above fail causing an error.  Tyring to convert to a string:

    if (@p.GetProperty<string>("isFeatured").Value == "1")
    {
    do seomthing
    }

    if (@p.GetProperty("isFeatured").Value == "True")
    {
    do seomthing
    }

    Also fails.

    Any help would be appreciated please.  

    Thanks

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 20, 2014 @ 06:48
    Fuji Kusaka
    0

    Hi Hugh,

    As Jeavon suggested in his last post it should be

    if(p.GetPropertyValue<bool>("isFeature")){ //something}
  • Hugh 30 posts 64 karma points
    Jan 20, 2014 @ 06:51
    Hugh
    0

    Hi Fuji

    No, tried that too...

    if(@p.GetPropertyValue<bool>("isFeature"))
    {
    }

    causes: Error loading Partial View script (file: ~/Views/MacroPartials/buslistingbycat.cshtml)

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 20, 2014 @ 06:55
    Fuji Kusaka
    0

    Can you please show the complete code ? @p stands for ? 

  • Hugh 30 posts 64 karma points
    Jan 20, 2014 @ 06:59
    Hugh
    0

    sure

    here you go:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @using System.Collections;
    
    @using System.Linq;
    
    @using System.Xml;
    
    @using System.Xml.Linq;
    
    @using umbraco.MacroEngines;
    
    @using umbraco;
    
    @{
    
    var articleContainerId = 1067;
    
    var topCat = Request.QueryString["topCat"];
    
    var subCat = Request.QueryString["subCat"];
    
    var suburb = Request.QueryString["sub"];
    
    var categoryId = CurrentPage.Id.ToString();
    
    var idToUse = string.Empty;
    
    
    
    int pageSize = 25;
    
       int page = 1; // The page we are viewing         
    
    if (!int.TryParse(Request.QueryString["page"], out page))
    
       {
    
            page = 1;
    
       }         
    
    if (!String.IsNullOrEmpty(topCat))
    
    {
    
    idToUse = topCat;
    
    }
    
    if (!String.IsNullOrEmpty(subCat))
    
    {
    
    idToUse = subCat;
    
    }
    
    if (String.IsNullOrEmpty(idToUse))
    
    {
    
    idToUse = categoryId;
    
    }
    
    IEnumerable<IPublishedContent> articles;
    
    if (String.IsNullOrEmpty(suburb))
    
    {
    
    articles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("cats").Contains(idToUse));
    
    }
    
    else
    
    {
    
    if(topCat=="1"){
    
    articles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue("suburb".ToUpper()).Equals(suburb.ToUpper()));
    
    }
    
    else
    
    {
    
    articles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("cats").Contains(idToUse)).Where(x => x.GetPropertyValue("suburb".ToUpper()).Equals(suburb.ToUpper()));
    
    }
    
    }
    
    int totalNodes = articles.Count();
    
       int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
    
    if (page > totalPages)
    
       {
    
           page = totalPages;  
    
       }
    
       else if (page < 1)
    
       {
    
           page = 1;
    
       }
    
    foreach (var p in articles.Skip((page - 1) * pageSize).Take(pageSize))
    
    {
    
    var advImgStr = @p.GetProperty("photos").Value.ToString().Split(',');
    
    var selectedMedia = Umbraco.Media(@advImgStr[0]);
    
    var bId = @p.GetProperty("broker").Value;
    
    var strPrice = @p.GetProperty("salePrice").Value;
    
    <article>
    
    <a href="@p.Url" class="single-image picture">
    
    <img src="/[email protected]&width=190&height=115&Align=center&Crop=Resize" alt="@p.GetProperty("pageHeading").Value" title="@p.GetProperty("pageHeading").Value">
    
    </a>
    
    <div class="detailed">
    
    <h6 class="title-item">
    
    
    
    <a href="@p.Url">@p.GetProperty("pageHeading").Value</a>
    
    </h6>
    
    @if (@p.GetProperty("salePrice").Value == "0")
    
    {
    
    <span class="price">TBA</span>
    
    
    
    } else
    
    {
    
    <span class="price">[email protected]("salePrice").Value</span>
    
    }
    
    <div class="clear"></div>
    
    <ul class="list-entry">
    
    <li><b class="label">Reference:</b><span>@p.GetProperty("propertyReference").Value</span></li>
    
    <li><b class="label">Location:</b><span>@p.GetProperty("suburb").Value @p.GetProperty("state").Value </span></li>
    
    <li><b class="label">Broker:</b><span> @Umbraco.RenderMacro("BrokerInfoById", new {[email protected]()})  </span></li>
    
    </ul><!--/ .list-entry-->
    
    <a href="@p.Url" class="button orange">Details</a>
    
    </div><!--/ .detailed-->
    
    </article>
    
    }
    
    <div class="wp-pagenavi clearfix">
    
    <div style="padding-bottom:10px;"><span class="pages">Page @page of @totalPages</span></div>
    
        @for (int p = 1; p < totalPages + 1; p++)
    
        {   
    
    var top1Cat = Request.QueryString["topCat"];
    
    var subCat1 = Request.QueryString["subCat"];
    
    var suburb1 = Request.QueryString["sub"];
    
            string selected = (p == page) ? "selected" : String.Empty;
    
    if (@selected == "selected")
    
    {
    
    <span class="current">@p</span>
    
    }
    
    else
    
    {
    
    string strURL = "?page=" + @p;
    
    @strURL = @strURL + "&topCat=" + @top1Cat;
    
    @strURL = @strURL + "&sub=" + suburb1;
    
    @strURL = @strURL + "&subCat=" + subCat1;
    
    
    
    <a class="page" href="@strURL" title="Go to page @p of results">@p</a>
    
    }
    
        }</div><!--/ .wp-pagenavi-->
    
    if (articles.Count() == 0)
    
    {
    
    <p>There are no listings in this category</p>
    
    }
    
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 20, 2014 @ 10:46
    Jeavon Leopold
    0

    Hi Hugh,

    First off, please find a fixed up version of your complete script:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Linq;
    @{
        var articleContainerId = 1067;
        var topCat = Request.QueryString["topCat"];
        var subCat = Request.QueryString["subCat"];
        var suburb = Request.QueryString["sub"];
        var categoryId = CurrentPage.Id.ToString();
        var idToUse = string.Empty;
        int pageSize = 25;
        int page = 1; // The page we are viewing
    
        if (!int.TryParse(Request.QueryString["page"], out page))
        {
            page = 1;
        }
    
        if (!string.IsNullOrEmpty(topCat))
        {
            idToUse = topCat;
        }
    
        if (!string.IsNullOrEmpty(subCat))
        {
            idToUse = subCat;
        }
    
        if (string.IsNullOrEmpty(idToUse))
        {
            idToUse = categoryId;
        }
    
        IEnumerable<IPublishedContent> articles;
        if (string.IsNullOrEmpty(suburb))
        {
            articles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("cats").Contains(idToUse));
        }
    
        else
        {
            if (topCat == "1")
            {
                articles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue("suburb".ToUpper()).Equals(suburb.ToUpper()));
            }
    
            else
            {
                articles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("cats").Contains(idToUse)).Where(x => x.GetPropertyValue("suburb".ToUpper()).Equals(suburb.ToUpper()));
            }
        }
    
        int totalNodes = articles.Count();
        int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
    
        if (page > totalPages)
        {
            page = totalPages;
        }
    
        else if (page < 1)
        {
            page = 1;
        }
    
        foreach (var p in articles.Skip((page - 1) * pageSize).Take(pageSize))
        {
            var advImgStr = p.GetPropertyValue<string>("photos").Split(',');
            var selectedMedia = Umbraco.Media(advImgStr[0]);
            var bId = p.GetPropertyValue("broker");
            var strPrice = p.GetPropertyValue("salePrice");
            <article>
    
                <a href="@p.Url" class="single-image picture">
                    <img src="/[email protected]&width=190&height=115&Align=center&Crop=Resize" alt="@p.GetPropertyValue("pageHeading")" title="@p.GetPropertyValue("pageHeading")">
                </a>
    
                <div class="detailed">
                    <h6 class="title-item">
                        <a href="@p.Url">@p.GetPropertyValue("pageHeading")</a>
                    </h6>
    
                    @if (p.GetPropertyValue<string>("salePrice") == "0")
                    {
                        <span class="price">TBA</span>
                    }
                    else
                    {
                        <span class="price">[email protected]("salePrice")</span>
                    }
    
                    <div class="clear"></div>
                    <ul class="list-entry">
                        <li><b class="label">Reference:</b><span>@p.GetPropertyValue("propertyReference")</span></li>
                        <li><b class="label">Location:</b><span>@p.GetPropertyValue("suburb") @p.GetPropertyValue("state") </span></li>
                        <li><b class="label">Broker:</b><span> @Umbraco.RenderMacro("BrokerInfoById", new { BrokerId = bId.ToString() })  </span></li>
                    </ul><!--/ .list-entry-->
                    <a href="@p.Url" class="button orange">Details</a>
                </div><!--/ .detailed-->
            </article>
    
        }
    
        <div class="wp-pagenavi clearfix">
            <div style="padding-bottom:10px;"><span class="pages">Page @page of @totalPages</span></div>
            @for (int p = 1; p < totalPages + 1; p++)
            {
                var top1Cat = Request.QueryString["topCat"];
                var subCat1 = Request.QueryString["subCat"];
                var suburb1 = Request.QueryString["sub"];
                string selected = (p == page) ? "selected" : string.Empty;
    
                if (selected == "selected")
                {
                    <span class="current">@p</span>
                }
                else
                {
                    string strURL = "?page=" + p;
                    strURL = strURL + "&topCat=" + top1Cat;
                    strURL = strURL + "&sub=" + suburb1;
                    strURL = strURL + "&subCat=" + subCat1;
                    <a class="page" href="@strURL" title="Go to page @p of results">@p</a>
                }
    
            }
        </div><!--/ .wp-pagenavi-->
    
        if (articles.Count() == 0)
        {
            <p>There are no listings in this category</p>
        }
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 20, 2014 @ 11:02
    Jeavon Leopold
    0

    Next up, the boolean if. Should be like this:

    if (p.GetPropertyValue<bool>("isFeatured"))
    {
    //do something
    }
    

    Two important points, you cannot have a @ in the code e.g. if (@p.GetPropertyValue second point you should alsways use GetPropertyValue("thing") not GetProperty("thing").Value.

    Jeavon

  • Hugh 30 posts 64 karma points
    Jan 20, 2014 @ 11:14
    Hugh
    0

    Thanks Jeavon.  I will try these now.  Sorry to keep asking, but one more.  This Partial Macro is driving me nutts.  I am tryint to format a datetime and it keeps giving me an error.

     <li>
    <span class="m-tag">@p.GetPropertyValue("PostDate")</span>
    </li>

    gives me: 1/18/2014 12:00:00 A 

    i have tried: @p.GetPropertyValue<DateTime>("newsPublishDate").ToString("MMMM dd, yyyy") but it fails....

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 20, 2014 @ 11:24
    Jeavon Leopold
    0

    No problem Hugh, because of the type conversion, you need some additional brackets:

     @(p.GetPropertyValue<DateTime>("newsPublishDate")).ToString("MMMM dd, yyyy")
    
  • Hugh 30 posts 64 karma points
    Jan 20, 2014 @ 11:33
    Hugh
    0

    Hi Jeavon, that did not work.  It gave me the following 1/19/2014 12:00:00 AM.TOSTRING("MMMM DD, YYYY")

    here is the code for your refenrence:

    foreach (var p in articles)

    {

    <li>

                        <span class="m-tag"> @(p.GetPropertyValue<DateTime>("newsPublishDate")).ToString("MMMM dd, yyyy")</span>                    

                        <a href="#" class="title">@p.GetProperty("pageHeading").Value</a>

                        <a href="#" class="author" title="Post by @p.CreatorName">@p.CreatorName</a>

                        <p>@p.GetProperty("smallDescription").Value</p>

                    </li>

     

    }

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 20, 2014 @ 11:39
    Jeavon Leopold
    0

    Ah sorry, bracket in the wrong place:

    <li>
        <span class="m-tag"> @(p.GetPropertyValue<DateTime>("newsPublishDate").ToString("MMMM dd, yyyy"))</span>
        <a href="#" class="title">@p.GetPropertyValue("pageHeading")</a>
        <a href="#" class="author" title="Post by @p.CreatorName">@p.CreatorName</a>
        <p>@p.GetPropertyValue("smallDescription")</p>
    </li>
    

    Remember to use GetPropertyValue not GetProperty

  • Hugh 30 posts 64 karma points
    Jan 20, 2014 @ 16:48
    Hugh
    0

    Thanks Jeavon.  You have been a big help!!!!  Did you see my last post here: http://our.umbraco.org/forum/developers/razor/47622-Partial-View-Macro-and-UmbracoTypedContent-Lambda-Issue about my DropDown state issue?  Here it is incase you did not see is:

     

    hHi all

    I have another issue.  I created a dropdown List datatype for State.  Vales stored are NSW, QLD, NT, WA, ACT, VIC, SA, TAS. 

    My query is:

    vararticles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("state").Contains("VIC"));

    But this is causing an error: Error loading Partial View script (file: ~/Views/MacroPartials/advancedSearchListing.cshtml)  The data stored in the xml output looks like this:       <state><![CDATA[VIC]]></state> 

    I have tried: vararticles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("state").Contains("VIC"));

    and vararticles = Umbraco.TypedContent(articleContainerId).Children.Where(x => x.GetPropertyValue<string>("state".ToUpper()).Contains("VIC".ToUpper())); But nothing seems to work. 

    Any ideas please?

Please Sign in or register to post replies

Write your reply to:

Draft