Question: replacing html helpers by tag helpers

Question

replacing html helpers by tag helpers

Answers 1
Added at 2016-12-26 05:12
Tags
Question

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.

Answers to

replacing html helpers by tag helpers

nr: #1 dodano: 2016-12-26 06:12

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/>
}
Source Show
◀ Wstecz