Combining Code and Text in HTML Attribute with Raz

2019-06-24 14:16发布

I am creating a bunch of <div> elements using a foreach loop with Razor syntax. Right now I have this:

@foreach (var item in Model)
{
    <div class="grid_6 listColumn" id="team_@item.TeamID">
    ...
    </div>
}

Basically I want the div identifiers to be labeled by the value in item.TeamID like:

team_1 team_2 team_3

The syntax I currently have doesn't recognize the code portion. I also tried id="team_@:item.TeamID" but it throws an error. However, id="team_ @item.TeamID" works fine, but I don't want that space in there. I'm pretty new to Razor, is there an easy way to do this?

1条回答
手持菜刀,她持情操
2楼-- · 2019-06-24 15:03

Try this:

<div class="grid_6 listColumn" id="@("team_" + item.TeamID)">
    ...
</div>
查看更多
登录 后发表回答