[Solved] Getting loop value outside the loop MVC C# [closed]


Your question is a bit vague but I’m assuming you want to assign the first visited class found (if any)

    var class_name = new string[] {}; // I changed this line just to comply with coding best practices

    @foreach (string items in str_array) 
                            // str_array I am getting like [0] = 1
                            //                             [1] = 2 
        {
            if (items.ToString() == class_id.ToString())
            {
                class_name = new string[] { "visited" };
                break;
            }
            else
            {
                class_name = new string[] { "NotVisited" };   
            }
        }

    @Html.ActionLink("test", "R_Class", "R_Class", null, new { @class = string.Format("{0}", class_name), onclick = "return false;" }) 

If my assumption is true and if when the class is not ‘visited’ it’s supposed to be ‘NotVisited’ then you can have a less verbose code like this:

    var class_name = new string[] { "NotVisited" }; 
    @foreach (string items in str_array) 
        {
            if (items.ToString() == class_id.ToString())
            {
                class_name = new string[] { "visited" };
                break;
            }
        }

    @Html.ActionLink("test", "R_Class", "R_Class", null, new { @class = string.Format("{0}", class_name), onclick = "return false;" }) 

2

solved Getting loop value outside the loop MVC C# [closed]