Copied to clipboard

Flag this post as spam?

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


  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 22:36
    Jacob Phillips
    0

    if comparison breaks partial view macro

    Umbraco 6.1.6

    A simple thing I want to do, just compare two values.

    I got a value I pass in to my partial view macro:

    var numberToDisplay = Model.MacroParameters["numberToDisplay"];
    

    Then later, I am looping through a collection inside of foreach block.

    @foreach (var item in myCollection)
    {
    
    }
    

    I can print both of these values individually in my foreach block:

    <p>@numberToDisplay</p>
    

    and

    <p>@item.Index()</p>
    

    Where @item.Index() does increment. But when I try to compare them, like this (still inside a foreach block):

    if(item.Index() < numberToDisplay)
    {
     <p>do some stuff</p>
    }
    

    It breaks.

    I have tried casting the values to an Int before comparing, like this:

    int max = int.Parse(item.Index());
    int pos = int.Parse(numberToDisplay);
    

    But I can't even do that w/o it breaking.

    Can't I just do a simple numeric comparison in Razor?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 22, 2014 @ 23:55
    Jeavon Leopold
    0

    Hi Jacob,

    This seems to work ok:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var myCollection = CurrentPage.AncestorOrSelf(1).Children;
        var numberToDisplay ="2";
    
        foreach (var item in myCollection)
        {
    
            if(item.Index() < int.Parse(numberToDisplay))
            {
             <p>do some stuff</p>
            }   
    
        }
    }
    

    Jeavon

  • Jacob Phillips 34 posts 84 karma points
    Jan 23, 2014 @ 00:10
    Jacob Phillips
    0

    Hmmm,

    The only way I can get it to work is like this:

    foreach (var item in myCollection)
    {
    
     var testit = "2";
     @if(item.Index() < int.Parse(testit))
     {
      <p>some stuff</p>
     }
    }
    

    Obviously, that says there is something strange about the var numberToDisplay.

    numberToDisplay is set like this:

    var numberToDisplay = Model.MacroParameters["numberToDisplay"];
    

    The scope seems global to the partial view since it is defined at the top. And I can confirm it is populated by just echoing it: @numberToDisplay

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 23, 2014 @ 00:20
    Jeavon Leopold
    100

    Maybe try var numberToDisplay = Model.MacroParameters["numberToDisplay"].ToString();

  • Jacob Phillips 34 posts 84 karma points
    Jan 23, 2014 @ 00:39
    Jacob Phillips
    0

    That's it. Thank you so much.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 23, 2014 @ 12:15
    Jeavon Leopold
    0

    You're welcome, it is required because Model.MacroParameters["numberToDisplay"] returns a object type.

Please Sign in or register to post replies

Write your reply to:

Draft