23

I have the following table located in a view within a controller named Student (/Student/Details/1):

    @foreach (var item in Model.Enrollments)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Course.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Grade)
            </td>
        </tr>
    }

I would like to make each table definition into a link that takes me to a view within a controller named Course (/Course/Details/1).

I have tried things along the lines of:

@Html.ActionLink(Html.DisplayFor(modelItem => item.Course.Title, "Details", "Course"))

in place of

@Html.DisplayFor(modelItem => item.Course.Title)

Which does not compile. How would I appropriately display my model's title along with a link to the details of the referenced title?

Ecnalyr
  • 5,792
  • 5
  • 43
  • 89

3 Answers3

40

If I understand right your question, you want a link with the text of the course.

This should work:

  @Html.ActionLink(item.Course.Title, "Details", "Course")

If you want to pass the ID of the course to the controller (assuming your routing rules are set correctly and the Id is something like: item.Course.Id)

  @Html.ActionLink(item.Course.Title, "Details", "Course", new { Id = item.Course.Id }, null /* html attributes */)

If you need to use the UIHint attribute on the property, to add extra formatting, you can use this

  <a href="@Url.Action("Details", "Course", new { Id=item.Course.Id})">@Html.DisplayFor(modelItem => item.Course.Title)</a>
Martin Eyles
  • 171
  • 13
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • I believe the second bit of code is exactly what I needed. Thank you. – Ecnalyr Mar 06 '12 at 13:44
  • Actually, I'm having a bit of trouble. I used this code: @Html.ActionLink(item.Course.Title, "Details", "Course", new { id= item.Course.CourseID }) but every link generated links specifically to this: /Student/Details/2?Length=6 Why would this happen? I do not know where the "?Length=6" comes from, nor why it is still going through my /Student controller instead of my /Course controller. – Ecnalyr Mar 06 '12 at 13:48
  • Wait, I think I see my mistake. Will reply shortly. – Ecnalyr Mar 06 '12 at 13:52
  • I wrote the code "on the fly". I missed the null for the HtmlAttributes. Edited the answer. – Iridio Mar 06 '12 at 13:58
  • I fixed it by using: @Html.ActionLink(item.Course.Title, "Details", "Course", new { id= item.Course.CourseID }, "a") I had added the "a" at the end, and it forced it to use the proper controller name of "Course" - without the "a" it was using a different overload that did not allow the course to be assigned. I could not get it to link properly without the "a". Any tips on solving that bit of weirdness? (nothing appears functionally wrong, just a bit odd - I have no idea what the "a" is actually doing") – Ecnalyr Mar 06 '12 at 14:03
  • As stated in my previous comment llok at the corrected line of code. That should do it. @Html.ActionLink(item.Course.Title, "Details", "Course", new { Id = item.Course.Id }, null /* html attributes */) – Iridio Mar 06 '12 at 14:55
  • Actually, `DisplayFor` can return text that is [quite different](http://stackoverflow.com/a/16738096/11683) from just the property value. It would be better to use [`DisplayTextFor`](http://stackoverflow.com/q/3473624/11683) or `ValueFor` to get the text for the link. – GSerg Aug 14 '14 at 18:39
3

You forgot an ) after Html.DisplayFor(modelItem => item.Course.Title.

Maybe try adding a .ToString() to it might help.

Matt
  • 74,352
  • 26
  • 153
  • 180
kows
  • 121
  • 1
  • 7
0
// you want to use link with the displaying a course. you can use.

<a href = "@url.action("Details","course",new {Id = item.Course.Id}
@html.displayfor(m => m.Course.Title)</a>

// second approach

 @Html.ActionLink(item.Course.Title, "Details", "Course", new { Id = item.Course.Id })
saurav singh
  • 438
  • 6
  • 16
  • If you want to pass the ID of the course to the controller (assuming your routing rules are set correctly and the Id is something like: item.Course.Id) – saurav singh Jul 20 '17 at 07:56