replacing html helpers by tag helpers

1.4k views Asked by At

I want to replace html helpers by tag helpers in View.

For this I replacing: @model IEnumerable<Person>

by @model IList<Person>

and @Html.DisplayNameFor(model => model.Name)

by <label asp-for="@Model[0].Name"></label>

but what I should use instead: @Html.DisplayFor(modelItem => item.Name)?

<input asp-for="@item.Name" readonly /> ?

It doesn't look right.

UPDATE: Just to clarify question: @Html.DisplayNameFor and <label asp-for both generate labels in html.

@Html.DisplayFor generates just text. Which tag helper can replace it? I don't want to use input just to show text.

1

There are 1 answers

3
Ahmar On

When you use IList then you can access the property both ways.

@model IList<WebApp.Models.Person>

<input type="text" asp-for="@Model[0].Name" />

@foreach(var item in Model)
{
    <input type="text" asp-for="@item.Name" readonly/>

}

and if you used IEnumerable, you need to call ToList method to access the properties without using foreach

@model IList<WebApp.Models.Person>
@{
var model2 = Model.ToList();
}

<input type="text" asp-for="@model2[0].Name" />

@foreach(var item in Model)
{
  <input type="text" asp-for="@item.Name" readonly/>
}