0

How to convert/ use DisplayNameFor as hyperlink which redirects to next page

Here is my code :

 @model IEnumerable<MyProject.Models.Record>
 @using (Html.BeginForm())

 {
      @Html.ActionLink(Html.DisplayNameFor(model => model.id).ToHtmlString(), "ADDPage", new { model=> model.id })
}

Getting error at new { model=> model.id }. How do I get id as pass it into this new { model=> model.id }

Hob
  • 139
  • 1
  • 13

1 Answers1

0

Are you sure you aren't getting an error at Html.DisplayNameFor(model => model.id)? Your model is an IEnumerable<T> which doesn't have an id property.

Presumably, you are trying to loop through the items in your model, in which case it would look something like this:

@foreach(var record in Model)
{
    @Html.ActionLink(Html.DisplayNameFor(model => record.id), "ADDPage", new { record.id })
}
Jason P
  • 26,984
  • 3
  • 31
  • 45
  • Yes you are right getting an error at Html.DisplayNameFor(model => model.id). . Without using for loop can I not directly get the ID ? – Hob Jul 26 '18 at 18:33
  • I need to use it as like a header. But adding foreach loop i get N number of IDs – Hob Jul 26 '18 at 18:56
  • @Hob See the answer to this question: https://stackoverflow.com/questions/20807869/displaynamefor-from-listobject-in-model – Jason P Jul 26 '18 at 19:38