문제

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