Html.LabelFor statement does not display as assigned value

I show the Guarantor name inside the foreach statement using the Html.LabelFor helper.

However, I do not get the expected result.

Instead of displaying the actual value of the GuaranterName name, it simply prints as follows: The label value indicates the actual name of the field, which is: GuarantorName, while I need the actual name value for this field (for example, "Bill Yeager"), which I checked during debugging, has various different names.

During debugging, I see in my list that I have the actual names in the collection.

How can i do this?

Here is my look in the code:

@foreach (var item in Model.Guarantors) { <td> @Html.CheckBoxFor(model => item.isChecked) @Html.LabelFor(model => item.GuarantorName) </td> } 

If I try this, I get the following error:

 @Html.DisplayFor(model => item.GuarantorFirstName + item.GuarantorLastName) 

Exception Details: System.InvalidOperationException: Templates can only be used with field access, resource access, one-dimensional array index, or one-parameter custom indexer expressions.

 Source Error: Line 95: <td> Line 96: @Html.CheckBoxFor(model => item.isChecked) Line 97: @Html.DisplayFor(model => item.GuarantorFirstName + item.GuarantorLastName) Line 98: </td> Line 99: } 
+4
source share
2 answers

LabelFor displays the name of the property. You want DisplayFor to display the field value.

 @foreach (var item in Model.Guarantors) { <td> @Html.CheckBoxFor(model => item.isChecked) @Html.DisplayFor(model => item.GuarantorName) </td> } 
+20
source
 @foreach (var item in Model.Guarantors) { <td> @Html.CheckBoxFor(model => item.isChecked) @Html.DisplayFor(model => item.GuarantorName) </td> } And also we can use when used IList<Model> @for(int i=0;i<model.Guarantors.count();i++) { <td> @Html.CheckBoxFor(model =>model[i].isChecked) @Html.DisplayFor(model => model[i].GuarantorName) </td> } 
0
source

Source: https://habr.com/ru/post/1438084/


All Articles