Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
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
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:
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
Maybe try var numberToDisplay = Model.MacroParameters["numberToDisplay"].ToString();
var numberToDisplay = Model.MacroParameters["numberToDisplay"].ToString();
That's it. Thank you so much.
You're welcome, it is required because Model.MacroParameters["numberToDisplay"] returns a object type.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion
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:
Then later, I am looping through a collection inside of foreach block.
I can print both of these values individually in my foreach block:
and
Where @item.Index() does increment. But when I try to compare them, like this (still inside a foreach block):
It breaks.
I have tried casting the values to an Int before comparing, like this:
But I can't even do that w/o it breaking.
Can't I just do a simple numeric comparison in Razor?
Hi Jacob,
This seems to work ok:
Jeavon
Hmmm,
The only way I can get it to work is like this:
Obviously, that says there is something strange about the var numberToDisplay.
numberToDisplay is set like this:
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
Maybe try
var numberToDisplay = Model.MacroParameters["numberToDisplay"].ToString();
That's it. Thank you so much.
You're welcome, it is required because Model.MacroParameters["numberToDisplay"] returns a object type.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.