duckmike
duckmike

Reputation: 1036

mvc 5 - getting value into id from loop

So here is my cshtml file -

@model Portal.ViewModel.DocumentViewModel
@using Portal.Helpers

@foreach (var item in Model.Documents)
{
    <div id="{@item.id}linkdiv">
        text
    </div>
}

this loops over maybe 5-8 records and I need to uniquely identify each div that I add.

This sample results in id's of "{2}linkdiv"

What is the syntax to get id = "2linkdiv"?

Upvotes: 0

Views: 413

Answers (2)

DLeh
DLeh

Reputation: 24395

Alternative to Stephen's answer, you could reverse your naming convention:

@model Portal.ViewModel.DocumentViewModel
@using Portal.Helpers

@foreach (var item in Model.Documents)
{
    <div id="[email protected]">
        text
    </div>
}

Upvotes: 0

Stephen Reindl
Stephen Reindl

Reputation: 5819

try

@model Portal.ViewModel.DocumentViewModel
@using Portal.Helpers

@foreach (var item in Model.Documents)
{
    <div id="@(item.id)linkdiv">
        text
    </div>
}

The parents encapsulate the razor statement.

Upvotes: 2

Related Questions