ASP.NET mvc View with IEnumerable model and input tag helper
Asked Answered
F

2

7

In this official ASP.NET Core tutorial, I can use an Input Tag Helper as shown below. But due to a known model binding issue of form elements in a foreach loop, I want to use for loop instead. Question: If I were to replace @foreach (var item in Model) with @for (int i=0; i < Model.Count(); i++) in the following View. What would be my asp-for in <input asp-for="???" /> ? For some reason, intellisense is not recognizing, e.g, Model[i].BlogId or @Model[i].BlogId

@model IEnumerable<EFGetStarted.AspNetCore.NewDb.Models.Blog>

@{
    ViewBag.Title = "Blogs";
}

<h2>Blogs</h2>

<p>
    <a asp-controller="Blogs" asp-action="Create">Create New</a>
</p>

<table class="table">
    <tr>
        <th>Id</th>
        <th>Url</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                <input asp-for="@item.BlogId" />
            </td>
            <td>
                <input asp-for="@item.Url" />
            </td>
        </tr>
    }
</table>
Freshen answered 11/9, 2016 at 22:40 Comment(3)
Why do you need input form fields if they are not in a form ? If it is only for display purpose, you can simply print the value.Kathlyn
@Kathlyn This post is only for the purpose of explaining my question. In real project, I do need input form fields and I do want to experiment input tag helper with for loop because of the problem with foreach loop (as described here). Also, @model need to be IEnumerable.Freshen
You cannot use a for loop with IEnumerable<T> - it would need to be IList<T>Perpendicular
V
2

I had the same problem and solved it this way:

@for (int i = 0; i < Model.Translations.Count; i++)
{
    @Html.HiddenFor(m => m.Translations[i].LanguageId)
    <form-group-text asp-for="@Model.Translations[i].Content"/>
}

The important part is using the for loop, and accessing the asp-for via @Model.

That said, you should rethink, if your Model itself should be IEnumerable. Instead create a property for it. Also you need to make sure, your Collection is accessable via Index. You could make your property of type IList. When you assign an IEnumerable to it, you can call ToList before.

Verdieverdigris answered 14/2, 2018 at 4:23 Comment(0)
B
1
    @model IEnumerable<EFGetStarted.AspNetCore.NewDb.Models.Blog>

    @{
        ViewBag.Title = "Blogs";
    }

    <h2>Blogs</h2>

    <p>
        <a asp-controller="Blogs" asp-action="Create">Create New</a>
    </p>

    <table class="table">
        <tr>
            <th>Id</th>
            <th>Url</th>
        </tr>
    @for (int item = 0; item < Model.Count(); item++)
        {
           <tr>
              <td>
                <input name="@Model.ToList()[item].BlogId" />
              </td>
              <td>
                <input name="@Model.ToList()[item].Url" />
              </td>
          </tr>
       }
    </table>
Bolan answered 6/11, 2017 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.