Copied to clipboard

Flag this post as spam?

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


  • William Corry 34 posts 171 karma points
    Mar 27, 2015 @ 12:59
    William Corry
    0

    Nesting partial views inside a loop

    I have a lot of forach loops runing inside of each other in one big file and I am looking to make the code a little be more managable but I can figure out how to get it to work here is a small portion of the code:

    
    @using Umbraco.Web.Models
    @using Umbraco.Cms.Custom
    @using System.Text.RegularExpressions
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    var Content = CurrentPage.HomeRepositories.First().PageItems;
    }
    @foreach (var item in Content)
    {
    var id = Regex.Replace(item.Name, @"([+|.|\'|\# |!|\(|?|,| |>|<|&|;|\)])", "", RegexOptions.IgnoreCase).ToLower();


    @foreach (var row in item.Children.Where("NodeTypeAlias == \"Row\""))
    {

    @{
    int count = row.Children.Count();
    int cols = 1;
    switch (count) {
    case 1:
    cols = 12;
    break;
    case 2:
    cols = 6;
    break;
    case 3:
    cols = 4;
    break;
    case 4:
    cols = 3;
    break;
    default:
    cols = 6;
    break;
    }
    }
    @foreach (var column in row.Children.Where("NodeTypeAlias == \"HeaderIcon\""))
    {

    @{
    var links = column.Links;
    if (links.Any())
    {
    foreach (var link in links)
    {
    padtop-30} animated" data-animtype="fadeInDown" data-animrepeat="0" data-animdelay="0">




    @if (column.HasValue("header")){

    @column.Header

    }


    @Html.Raw(column.BodyText)

    }

    }
    else
    {
    padtop-30} animated" data-animtype="fadeInDown" data-animrepeat="0" data-animdelay="0">
    visible-md-block visible-lg-block}">
    fa-4x}else{white}">


    @if (column.HasValue("header")){

    @column.Header

    }

    @Html.Raw(column.BodyText)

    }
    }

    }
    @foreach (var column in row.Children.Where("NodeTypeAlias == \"HeaderSmallImage\""))
    {

    @{
    var links = column.Links;
    if (links.Any())
    {
    foreach (var link in links)
    {

    }

    }
    else
    {



    @if (column.HasValue("header")){



    @column.Header


    }

    @Html.Raw(column.BodyText)

    }
    }

    }

    }


    }

    What I want to do is have the inner foreaches inisde there own partial views ie having the following on its own.


    @{
    var links = column.Links;
    if (links.Any())
    {
    foreach (var link in links)
    {

    }

    }
    else
    {



    @if (column.HasValue("header")){



    @column.Header


    }

    @Html.Raw(column.BodyText)

    }
    }

    When I have tried using somehting like:
    @Html.Partial("Content/HeaderIcon", Model.Content)

    it throws an error on the call to <div class="col-sm-@cols"> which is defined in the parent viewany suggestiongs will be welcome

  • William Corry 34 posts 171 karma points
    Mar 27, 2015 @ 14:33
    William Corry
    0

    Arg getting html to display on the forums is a pain :(

    @using Umbraco.Web.Models
    @using Umbraco.Cms.Custom
    @using System.Text.RegularExpressions
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
        var Content = CurrentPage.HomeRepositories.First().PageItems;
    }

    @foreach (var item in Content)
    {
        var id = Regex.Replace(item.Name, @"([+|.|\'|\# |!|\(|?|,| |>|<|&|;|\)])", "", RegexOptions.IgnoreCase).ToLower();

    <section id="@id" class="page-section">

    <div class="container">

    @foreach (var row in item.Children.Where("NodeTypeAlias == \"Row\""))
                {
                    <div class="row">
                        @{
                            int count = row.Children.Count();
                            int cols = 1;
                            switch (count) {
                                case 1:
                                    cols = 12;
                                    break;
                                case 2:
                                    cols = 6;
                                    break;
                                case 3:
                                    cols = 4;
                                    break;
                                case 4:
                                    cols = 3;
                                    break;
                                default:
                                    cols = 6;
                                    break;
                            }
                        }
                        @foreach (var column in row.Children.Where("NodeTypeAlias == \"HeaderSmallImage\""))
                        {
                            <div class="col-sm-@cols">
                                @{
                                    var links = column.Links;
                                    if (links.Any())
                                    {
                                        foreach (var link in links)
                                        {
                                            <div class="row">
                                                <div class="col-md-offset-1 col-md-10">
                                                    <div class="content-box content-box-small">
                                                        <a href="@link.Url" title="@link.Name" target="@link.Target">
                                                        @if (column.HasValue("header")){
                                                            <h4 class="animated" data-animtype="animated-scale" data-animrepeat="0" data-animdelay="0s">
                                                                <i class="@column.Icon display-icon"></i>
                                                                @column.Header
                                                            </h4>
                                                        }
                                                        </a>
                                                        <div class="content-box-text animated" data-animtype="fadeInDown" data-animrepeat="0" data-animdelay="1s">
                                                            @Html.Raw(column.BodyText)
                                                           </div>
                                                    </div>
                                                </div>
                                            </div>
                                        }

                                    }
                                    else
                                    {
                                        <div class="row">
                                            <div class="col-md-offset-1 col-md-10">
                                                <div class="content-box content-box-small">
                                                    @if (column.HasValue("header")){
                                                        <h4 class="animated" data-animtype="animated-scale" data-animrepeat="0" data-animdelay="0s">
                                                            <i class="@column.Icon display-icon"></i>
                                                            @column.Header
                                                        </h4>
                                                    }
                                                    <div class="content-box-text animated" data-animtype="fadeInDown" data-animrepeat="0" data-animdelay="1s">
                                                        @Html.Raw(column.BodyText)
                                                       </div>
                                                </div>
                                            </div>
                                        </div>
                                    }
                                }
                            </div>
                        }

    </div>

    }

    </div>

    </section>

    }

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Mar 28, 2015 @ 16:20
    Alex Skrypnyk
    0

    Hi William,

    You can't init variable in parent view and than use it in the child, only Model is passed through the views.

    THanks

Please Sign in or register to post replies

Write your reply to:

Draft