Developer forum

Forum » Templates » [Razor] Using a concatenated variable with GetString

[Razor] Using a concatenated variable with GetString

Gaetan Di Caro
Reply

Hello,

I have several group fields which are numbered, so to avoid writing 20 times the same html code, I wanted to do a for loop :

@for (int i = 1; i <= 10; i++)
{
    tagName = "Ecom:Group:Field.Picture" + i.ToString() + ".Value.Clean";
    if (!string.IsNullOrWhiteSpace(GetString(tagName)))
    {
        <img src="@GetString(tagName)" />
    }
}

However this doesn't work. GetString gives me an empty string. If I put the tag name textually (i.e "Ecom:Group:Field.Picture1.Value.Clean", etc.) it works.

I tried to isolate the problem. This doesn't work either :

string one = "1";
string tag = "Ecom:Group:Field.Picture" + one + ".Value.Clean";
@designer.GetValue(tag)

Neither does this :

string tag = "Ecom:Group:Field.Picture" + "1.Value.Clean";
@designer.GetValue(tag)

However, this is ok :

string tag = "Ecom:Group:Field.Picture1.Value.Clean";
@designer.GetValue(tag)

 

This is a pretty weird behaviour...

Any idea ?

 

Thanks !


Replies

 
Nicolai Høeg Pedersen
Reply

Try using parenthesis: ("Ecom:Group:Field.Picture" + one + ".Value.Clean");

Or String.format("Ecom:Group:Field.Picture{0}.Value.Clean", "1");

 
Gaetan Di Caro
Reply

Hello Nicolai.

Thanks for your answer. I tried both and I'm afraid they don't work either :(

 
Gaetan Di Caro
Reply

And of course this doesn't work too :

@GetString("Ecom:Group:Field.Picture" + i.ToString() + ".Value.Clean")

 
Nicolai Høeg Pedersen
Reply

Hi Gaetan

Are you using the right object instance? In some of your examples you write @designer.GetValue(tag) others just @GetValue(tag)

I just tried this with several tags in my test site, and it works without any problems...

Can I see some more or all of the template?

 BR Nicolai

 
Gaetan Di Caro
Reply

Ok I'm not sure exactly what happened. I commented my code to write a clear demonstration of what worked and what didn't... And everything worked. Then I commented that and uncommented my previous code (which didn't work). And it worked right away. Maybe a cache problem or something like that.

 
Gaetan Di Caro
Reply

Ok I've noticed a pattern. It seems like I NEED to directly call at the picture I want somewhere for the it to work. Prepare to be entertained...

This doesn't work (no output at all):

@{
    var designer = GetLoop("AssociatedGroups").FirstOrDefault(x => x.GetString("Ecom:Group:Field.GroupType.Value.Clean") == "designer");
}

@for (int i = 1; i <= 10; i++)
{
    tagName = String.Format("Ecom:Group:Field.DesktopCarouselPicture{0}.Value.Clean", i.ToString());
    if (!string.IsNullOrWhiteSpace(designer.GetString(tagName)))
    {
        <img src="@designer.GetString(tagName)" alt="@designer.GetString("Ecom:Group:Field.Title")" />
    }
}

This works, but only for the first picture :

@{
    var designer = GetLoop("AssociatedGroups").FirstOrDefault(x => x.GetString("Ecom:Group:Field.GroupType.Value.Clean") == "designer");

   string useless = designer.GetString("Ecom:Group:Field.DesktopCarouselPicture1.Value.Clean");
}

@for (int i = 1; i <= 10; i++)
{
    tagName = String.Format("Ecom:Group:Field.DesktopCarouselPicture{0}.Value.Clean", i.ToString());
    if (!string.IsNullOrWhiteSpace(designer.GetString(tagName)))
    {
        <img src="@designer.GetString(tagName)" alt="@designer.GetString("Ecom:Group:Field.Title")" />
    }
}

Even more amazing, this works too :

@{
    var designer = GetLoop("AssociatedGroups").FirstOrDefault(x => x.GetString("Ecom:Group:Field.GroupType.Value.Clean") == "designer");

   //string useless = designer.GetString("Ecom:Group:Field.DesktopCarouselPicture1.Value.Clean");
}

@for (int i = 1; i <= 10; i++)
{
    tagName = String.Format("Ecom:Group:Field.DesktopCarouselPicture{0}.Value.Clean", i.ToString());
    if (!string.IsNullOrWhiteSpace(designer.GetString(tagName)))
    {
        <img src="@designer.GetString(tagName)" alt="@designer.GetString("Ecom:Group:Field.Title")" />
    }
}

 

 
Nicolai Høeg Pedersen
Reply

Hi Gaetan

Ah, that is because our code uses "TagExist" - meaning it will not render the tag if the tag is not in use. So if you do not have the full tagname in the code, TagExists returns false and the tag is not rendered....

BR Nicolai

 
Gaetan Di Caro
Reply

Ah, that explains it...

I've put this at the end of my template as a workaround :

@*

    Ecom:Group:Field.DesktopCarouselPicture1.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture2.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture3.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture4.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture5.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture6.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture7.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture8.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture9.Value.Clean
    Ecom:Group:Field.DesktopCarouselPicture10.Value.Clean
*@

 

 

You must be logged in to post in the forum