@ Html.ActionLink和@ Html.DisplayFor同时使用(不正确,但它描述了我想要做什么) [英] @Html.ActionLink and @Html.DisplayFor at the same time (not right, but it describes what I want to do)

查看:53
本文介绍了@ Html.ActionLink和@ Html.DisplayFor同时使用(不正确,但它描述了我想要做什么)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为Student(/Student/Details/1)的控制器内的视图中具有下表:

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>
    }

我想将每个表定义变成一个链接,该链接将我带到名为Course(/Course/Details/1)的控制器内的视图.

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).

我尝试了以下方法:

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

代替

@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?

推荐答案

如果我正确理解您的问题,则需要与课程文本的链接.

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

这应该有效:

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

如果您要将课程ID传递给控制器​​(假设您的路由规则设置正确,并且Id类似于:item.Course.Id)

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 */)

如果需要在属性上使用UIHint属性,以添加额外的格式,则可以使用此

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>

这篇关于@ Html.ActionLink和@ Html.DisplayFor同时使用(不正确,但它描述了我想要做什么)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆