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

前端 未结 3 1833
遇见更好的自我
遇见更好的自我 2021-02-12 09:38

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

    @foreach (var item in Model.Enrollments)
    {
        &         


        
相关标签:
3条回答
  • 2021-02-12 10:33
    // 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 })
    
    0 讨论(0)
  • 2021-02-12 10:35

    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>
    
    0 讨论(0)
  • 2021-02-12 10:40

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

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

    0 讨论(0)
提交回复
热议问题