@Html.ActionLink and @Html.DisplayFor at the same time (not right, but it describes what I want to do)
Asked Answered
U

3

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?

Uncaused answered 6/3, 2012 at 13:31 Comment(0)
G
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>
Germander answered 6/3, 2012 at 13:39 Comment(7)
I believe the second bit of code is exactly what I needed. Thank you.Uncaused
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.Uncaused
Wait, I think I see my mistake. Will reply shortly.Uncaused
I wrote the code "on the fly". I missed the null for the HtmlAttributes. Edited the answer.Germander
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")Uncaused
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 */)Germander
Actually, DisplayFor can return text that is quite different from just the property value. It would be better to use DisplayTextFor or ValueFor to get the text for the link.Aggrieved
S
3

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

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

Spread answered 6/3, 2012 at 13:38 Comment(0)
P
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 })
Panter answered 20/7, 2017 at 7:29 Comment(1)
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)Panter

© 2022 - 2024 — McMap. All rights reserved.