Reft
Reft

Reputation: 2433

MVC HTML Set img class name from a variable in view

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?

Upvotes: 1

Views: 1790

Answers (2)

Singh
Singh

Reputation: 554

@{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.

Upvotes: -1

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

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

OR

<img class='Edit@(rows)' src="~/Content/images/edit.png" />
..
rows++;

Upvotes: 2

Related Questions