Copied to clipboard

Flag this post as spam?

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


  • Brian McNally 13 posts 91 karma points
    Nov 11, 2014 @ 20:48
    Brian McNally
    0

    If statement Syntax

    I posted this a couple days ago but may have not explained it well. I have a searchresults.cshtml macroscript tied to a macro embedded on a page. It successfully returns search results, but if the metaDescription field is null for an item, then I get an error on the page "Error loading MacroEngine script (file: SearchResults.cshtml) " Rather than going through all my site pages and assets and verifying there is a metaDescription, I'd like to write an if statement saying something like this

    @if (page.Fields["metaDescription"].HasValue){
    
    <p>@page.Fields["metaDecription"]</p>
    }
    

    This statement and many similar ones still causes the same error..Any blaring obvious ideas or help with the correct syntax for this stament? thanks

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Nov 11, 2014 @ 20:54
    Dennis Aaen
    1

    Hi Brian,

    What if you do something like this.

    @if (page.HasValue("metaDescription"){
    <p>@page.Fields["metaDecription"]</p>
    }

    And I think that you could access the value of the field like this

    <p>@page.metaDecription</p>

    Hope this helps,

    /Dennis

  • Brian McNally 13 posts 91 karma points
    Nov 11, 2014 @ 21:00
    Brian McNally
    0

    Hi Dennis, thanks so much for the quick reply..Just tried that (even swapped out the actual value in the

    tag with just 'test' text and I get this: Error loading MacroEngine script (file: Search_Results.cshtml)

  • Brian McNally 13 posts 91 karma points
    Nov 11, 2014 @ 21:06
    Brian McNally
    0

    Also that doesn't work, just using the @page.MetaDescription..If this helps at all, below is the entire macropartial page..

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @using Examine;
    @using UmbracoExamine;
    
    
    @*get the search term from the querystring*@
    @{var searchTerm = Request.QueryString["search"];}
    @{var pageResults = ExamineManager.Instance.SearchProviderCollection["MySiteSearcher"].Search(searchTerm, true);}
    @{var videoResults = ExamineManager.Instance.SearchProviderCollection["ghxVideoSearcher"].Search(searchTerm, true);}
    @{var wpResults = ExamineManager.Instance.SearchProviderCollection["ghxWPSearcher"].Search(searchTerm, true);}
    @{var csResults = ExamineManager.Instance.SearchProviderCollection["ghxCSSearcher"].Search(searchTerm, true);}
    @{var brochureResults = ExamineManager.Instance.SearchProviderCollection["ghxPISearcher"].Search(searchTerm, true);}
    @{var webinarResults = ExamineManager.Instance.SearchProviderCollection["ghxWEBINARSearcher"].Search(searchTerm, true);}
    @{var ldResults = ExamineManager.Instance.SearchProviderCollection["ghxLeaderSearcher"].Search(searchTerm, true);}
    @{var prResults = ExamineManager.Instance.SearchProviderCollection["ghxPRSearcher"].Search(searchTerm, true);}
    
    <div class="container" style="margin-bottom:50px;">
      <div class="row">
        <h1>Search Results for "@searchTerm"</h1>
      </div>
      @if (
      @pageResults.TotalItemCount == 0
        && @videoResults.TotalItemCount == 0
        && @wpResults.TotalItemCount == 0
        && @csResults.TotalItemCount == 0
        && @brochureResults.TotalItemCount == 0
        && @webinarResults.TotalItemCount == 0
        && @ldResults.TotalItemCount == 0
        && @prResults.TotalItemCount == 0
    
    
      )
      {
      <div role="alert" class="alert alert-warning"> <strong>no results found matching "@searchTerm"</strong><br/>
        <br/>
        <ul>
          <li>Make sure all words are spelled correctly</li>
          <li>Try diferent keywords</li>
          <li>try more general keywords</li>
        </ul>
      </div>
      }
      <div> @*PAGES*@
        <div> @if (@pageResults.TotalItemCount > 0)
          {
          <div id="PAGE" class="assetCatHeader">pages</div>
          }
    
    
          @*TODO: FILTER OUT UMBRACONAVIHIDEPAGES*@
          @foreach (var page in pageResults) {
          <div class="[email protected]["umbracoNaviHide"]">
            <dl>
              <dt> <a href="@umbraco.library.NiceUrl(page.Id)">@page.Fields["ghxPageTitle"]</a></dt>
    
            </dl>
    
    
          </div>
          } </div>
    
    
    
    
        @*PRESS RELEASES*@
        <div>
    
            @if (@prResults.TotalItemCount > 0)
            {
    
                <div id="PR" class="assetCatHeader">press releases</div>
    
            }
    
            @foreach (var pr in prResults)
            {
    
                    <dl>
              <dt> <a href="@umbraco.library.NiceUrl(pr.Id)">@pr.Fields["assetTitle"]</a></dt>
            </dl>
    
    
    
    
            }
        </div>
    
    
    
    
    
        @*VIDEOS*@
        <div> @if (@videoResults.TotalItemCount > 0)
          {
          <div id="VID" class="assetCatHeader">Videos</div>
          }
    
    
          @foreach (var vid in videoResults)
          {
          <dl>
            <dt> <a href="@umbraco.library.NiceUrl(vid.Id)">@vid.Fields["assetTitle"]</a></dt>
          </dl>
          } </div>
        @*White Papers*@
        <div> @if (@wpResults.TotalItemCount > 0)
          {
          <div id="WP" class="assetCatHeader">white papers</div>
          }
    
    
          @foreach (var wp in wpResults)
          {
          <dl>
            <dt> <a href="@umbraco.library.NiceUrl(wp.Id)">@wp.Fields["assetTitle"]</a></dt>
          </dl>
          } </div>
        @*Case Studies*@
        <div> @if (@csResults.TotalItemCount > 0)
          {
          <div id="CS" class="assetCatHeader">case studies</div>
          }
    
    
          @foreach (var cs in csResults)
          {
          <dl>
            <dt> <a href="@umbraco.library.NiceUrl(cs.Id)">@cs.Fields["assetTitle"]</a></dt>
          </dl>
          } </div>
    
    
           @*brochures*@
        <div> @if (@brochureResults.TotalItemCount > 0)
          {
          <div id="PI" class="assetCatHeader">product information</div>
          }
    
    
          @foreach (var brochure in brochureResults)
          {
          <dl>
            <dt> <a href="@umbraco.library.NiceUrl(brochure.Id)">@brochure.Fields["assetTitle"]</a></dt>
          </dl>
          } </div>
    
    
           @*WEBINARS*@
        <div>
    
            @if (@webinarResults.TotalItemCount > 0)
            {
    
                <div id="WEB" class="assetCatHeader">webinars</div>
    
            }
    
            @foreach (var webinar in webinarResults)
            {
    
          <dl>
            <dt> <a href="@umbraco.library.NiceUrl(webinar.Id)">@webinar.Fields["assetTitle"]</a></dt>
          </dl>        
            }
    
        </div>
    
     @*LEADERSHIP*@
        <div>
    
    
    
            @if (@ldResults.TotalItemCount > 0)
            {
    
                <div id="LEADER" class="assetCatHeader">Leadership</div>
    
            }
    
            @foreach (var leader in ldResults)
            {
    
       <dl>
            <dt><a href="@umbraco.library.NiceUrl(leader.Id)">@leader.Fields["leaderName"]</a></dt>
            <dd>@leader.Fields["leaderTitle"]</dd>
          </dl>   
            }
    
        </div>
    
    
    
    
    
    
      </div>
    </div>
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Nov 11, 2014 @ 21:07
    Dennis Aaen
    1

    Okay does it any difference if you do it this way.

    @if(!string.IsNullOrEmpty(page.Fields["metaDecription"])){

    <p>@page.Fields["metaDecription"]</p>
    }

    Or you could try:

    @if(page.Fields["metaDescription"]!= null){

    <p>@page.Fields["metaDecription"]</p>
    }

    Hope this helps,

    /Dennis

  • Brian McNally 13 posts 91 karma points
    Nov 11, 2014 @ 21:23
    Brian McNally
    0

    Thanks Dennis, I would think those would work right? Not sure what's going on here. Both of them throw the same error on the page: Error loading MacroEngine script (file: Search_Results.cshtml)

    Not sure what the deal is or how to go about fixing it. Right now, I'm just not showing the metaDescriptions, but it would be nice to have logic in there showing that info if it exists.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Nov 11, 2014 @ 21:39
    Jan Skovgaard
    1

    Hi Brian

    May I ask what exact version of 7 you're using? And do you get any further details if you look in the trace log in /app_data/logs? Watch for any message marked with "error" that might relate to this.

    /Jan

  • Brian McNally 13 posts 91 karma points
    Nov 11, 2014 @ 21:44
    Brian McNally
    0

    Hi Jan,

    Umbraco version 7.1.6 assembly: 1.0.5350.25714

    2014-11-11 13:31:12,402 [5] WARN umbraco.macro - [Thread 7] Error loading MacroEngine script (file: SearchResults.cshtml, Type: ''. Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.getItem(TKey key) at ASP.PagemacroScriptsSearchResultscshtml.Execute() in c:\inetpub\GHXroot\MacroScripts\SearchResults.cshtml:line 61 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.WebPages.WebPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage) at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage) at umbraco.macro.loadMacroScript(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)

  • Brian McNally 13 posts 91 karma points
    Nov 11, 2014 @ 21:47
    Brian McNally
    0

    Another odd thing I just noticed is that the following if statement kind of works (if the results actually have value for metaDescription)

    @if(!string.IsNullOrEmpty(page.Fields["metaDescription"])){
                <dd>@page.Fields["metaDescription"]</dd>
            }
    

    I say "Kind of", because it will diplay the description, but if it's trying to evaluate a record what doesn't have a value for that then it throws that error.

Please Sign in or register to post replies

Write your reply to:

Draft