Domanda

I got this loop going through a list of albums:

for (int i = 0; i < Model.Albums.Count; i++)
  {
 <div class="form-group">
  @Html.LabelFor(x => Model.Albums[i].AlbumCovers)
 </div>

Is it possible to get a hold if the [i]?

The [i] is an int representing the index and i would like to store it in a variable if possible?

Thanks!

È stato utile?

Soluzione

You don't have to store the index to create a link for a mode item. What you can do is generate an action link with each iteration of your model. Here is a sample for you:

for (int i = 0; i < Model.Albums.Count; i++)
  {
    @Html.ActionLink("Edit Record", "Edit", new {Id=i})
  }

Here Edit Record is the text for the link and Edit is the controller action name, the Id is the parameter to edit action of the controller.

Altri suggerimenti

It's not clear exactly what you are asking, i is already a variable which contains an integer but you can do this if you want:

for (int i = 0; i < Model.Albums.Count; i++)
{
    int index = i;
    @Html.LabelFor(x => Model.Albums[i].AlbumCovers)
}

If you want to capture a variable for the album, you can do this:

for (int i = 0; i < Model.Albums.Count; i++)
{
    Album album = Model.Albums[i];
    @Html.LabelFor(x => album.AlbumCovers)
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top