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!

有帮助吗?

解决方案

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.

其他提示

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)
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top