Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Jan 21, 2014 @ 23:44
    Craig O'Mahony
    0

    Error in Razor when inserting HTML

    Hi all,

    Apologies for the rather long code block but I've included it all in case it helps someone find the error that I'm receiving!

    I have the below code in Razor which is working fine:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{          
    var sortedList = Model.Children;  
    foreach(var page in  sortedList)
    {  
    @page.bodyText
    var childList = page.Children;
    foreach(var child in childList) 
    {
    <div class="treatment">
    <div class="treatmentHeader">
    <div class="large-8 columns no-left-padding">
    <a href="#">@child.mainTitle</a>
    </div>
    <div class="large-4 columns">
    <div class="treatmentPrice">@child.mainPrice</div>
    </div>
    @if (child.ndTitle != "")
    {
    <div class="large-8 columns no-left-padding">
    <a href="#">@child.ndTitle</a>
    </div>
    <div class="large-4 columns">
    <div class="treatmentPrice">@child.ndPrice</div>
    </div>
    }
    <div class="row">
    <div class="large-12 columns">
    <p>@child.type</p>
    </div>
    </div>
    </div>
    @child.bodyText
    </div>
    }
    }       
    }

    My problem comes when I try to insert some more html after the line

    The code that I'm inputting is <div class="large-6 columns">  and I'm putting the closing div just prior to the code block end. The error that I receive is "The name 'child' does not exist in the current context" and I am stumped!!

    Could anyone help me please?

    Thanks,

    Craig

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 22, 2014 @ 05:27
    Fuji Kusaka
    0

    Hi Craig,

    You are getting this error message since you are trying to call some properties which is in the second  loop that.

    foreach(var child in childList){}

    Are you just trying to add some Html code there are is there any property you want to display ?

     

     

     

     

  • Craig O'Mahony 364 posts 918 karma points
    Jan 22, 2014 @ 10:11
    Craig O'Mahony
    0

    Hi Fuji,

    That's right I just want to return some HTML just before and after the loop.

    Thanks,

    Craig

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 22, 2014 @ 11:10
    Fuji Kusaka
    0

    This should get you working

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{         
                var sortedList = Model.Children; 
            var content = Model.AncestorOrSelf(1).Descendants("verandaResortsRooms").Where("Visible").Items;
           
                foreach(var page in  sortedList)
                { 
                    @page.Name @page.NodeTypeAlias<br/>
                           var childList = page.Children;
                               foreach(var child in childList)
                                { 
                                         <div class="treatment">
                                                <div class="treatmentHeader">
                                                          <div class="large-8 columns no-left-padding"><a href="#">@child.mainTitle</a></div>
                                                        <div class="large-4 columns"><div class="treatmentPrice">@child.mainPrice</div></div>
                                                           
                                                @if(child.ndTitle != ""){
                                                    <div class="large-8 columns no-left-padding"><a href="#">@child.ndTitle</a></div>
                                                     <div class="large-4 columns"><div class="treatmentPrice">@child.ndPrice</div></div>
                                                 }
                                         <div class="row">
                                              <div class="large-12 columns"><p>@child.type</p></div>
                                     </div>
                             </div>
                                     @child.bodyText
                              </div>
                             }
                    <div class="large-6 columns">
                         @Html.Raw("Some Text")
                </div>
            }      
           
    }

    Hope this helps

Please Sign in or register to post replies

Write your reply to:

Draft