1

I want to set class name to img tags and the name should be string + variable from my view, (Edit + variable).

@{int rows = 1;}

@foreach (var file in Model)
{  
    <img class='"Edit"+@rows' src="~/Content/images/edit.png" />
    FileIconRow++;
}

So the class names should be Edit1, Edit2, Edit3 etc for every model it loops through.

Is this even possible? I'm not getting it to work, am i doing something wrong?

2 Answers 2

2
<img class='Edit_@rows' src="~/Content/images/edit.png" />
...
rows++;

OR

<img class='Edit@(rows)' src="~/Content/images/edit.png" />
..
rows++;
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome thanks, the first one didn't work but the second one did. Faaanx!
first one output: Edit_1, Edit_2, ...
@downvoter, you should explain why did you downvote?
-1
@{int rows = 1;}
@foreach (var file in Model)
{  
    <img class='"Edit"+@rows' src="~/Content/images/edit.png" />

    rows++;
}

Why are you incrementing FileIconRow++? You have taken @row variable as counter you should be increment @row and this is to be used in class name as shown above.

2 Comments

Its not copy of code . @jww was incrementing FileIconRow++; But he should be increment the row variable that he have taken as a counter. I think you should got my point.
@jww thanks you saw difference :) please upvote my answer as by mistake people are downvote me but my answer is correct. And thnks for suggestion I have edited my answer with explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.