简体   繁体   English

使用for循环在umbraco中传递数组的字符串值

[英]using for-loop to pass string values of an array in umbraco

I am trying to pass the array value one by one to the image 我试图将数组值一一传递给图像

<img src="@crops.Find("@name", "boxes[0]").url" alt="" />

for instance for first time the boxes[0] = box-1x1 , similarly when the 例如第一次,box [0] = box-1x1,类似

foreach (var node in Model.Children)

loop runs second time the box value should be boxes[1] which is box-2x1 , i just can think of a way how to run for loop to pass these values 8 times as the array size is 0-8 循环第二次运行时,box值应该是box [1],它是box-2x1,我只是想出一种方法来运行循环以将这些值传递8次,因为数组大小为0-8

 @{ @* Initialize box styles:list.Add(node.UrlName);*@
    string[] boxes = { "box-1x1", "box-2x1", "box-1x1" , "box-2x1" ,"box-2x2", "box-1x2", "box-1x1" , "box-2x1","box-1x1" };
    var list = new List<string>();
    foreach (var node in Model.Children)
        {
            dynamic croping = @Model.mainImage.mediaItem.Image.mediaCropper;
            foreach (dynamic crops in croping)
                {   
                    if (crops.GetType().ToString() != "System.String")
                            {   
                                <img src="@crops.Find("@name", "boxes[0]").url" alt="" />
                            }   
                }   
        }

 }

Can any one provide me with any assistance or suggestion. 谁能为我提供任何帮助或建议。 Any help will be appreciated Thanks 任何帮助将不胜感激谢谢

If you want an index in your loop, use a for loop, not foreach . 如果要在循环中使用索引,请使用for循环,而不要使用foreach Alternatively, define an int index = 0 variable before your foreach loop and advance it by 1 after each iteration. 或者,在foreach循环之前定义一个int index = 0变量,并在每次迭代后将其前进1。

For example: 例如:

int index = 0;
foreach (var node in Model.Children)
{
    // Do your stuff
    // ...
    <img src="@boxes[index]" />
    // ...

    index++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM