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

送分小仙女□ 提交于 2019-12-05 07:22:25
    @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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!