html - MVC Razor View Engine,在引号之间添加逻辑

标签 html asp.net-mvc razor

在将 Razor 与 MVC 结合使用时我真的很陌生,到目前为止我真的很喜欢它。

我想做的一件小事是将变量值写入 HTML 中的属性。 例如,如果我有以下内容:

    <table style="width: 100%">
        <tr>
            <th>
                ID
            </th>
            <th>
                Name
            </th>
            <th>
                Value
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr id="ID@item.id">
                <td>
                    @item.id
                </td>
                <td>
                    @item.name
                </td>
                <td>
                    @item.value
                </td>
                <td>
            </tr>
        }
    </table>

我希望所有标签的 ID 都等于“ID[插入项目的 ID]”,但我得到的 ID 等于“ID@item.id”。

如何在 HTML 引号之间写入变量名?

谢谢!

最佳答案

此模式被识别为电子邮件地址。执行以下操作来解决此问题:

<tr id="ID@(item.id)">

关于html - MVC Razor View Engine,在引号之间添加逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266512/

相关文章:

javascript - 用 Javascript 生成时髦的角色?

html - 将鼠标悬停在元素上时移动其他元素

asp.net-mvc - MVC5 : Can @Html. TextBoxFor(或EditorFor)从模型的数据注释中获取其MaxLength?

jquery - MVC 将 "#"附加到查询字符串

asp.net-mvc - HTML.Raw 的 MVC @helper 错误

jquery - 无法缩进表中的 th 和 td 元素

html - 必需属性 HTML5

javascript - 在 Stimulus JS 中处理集合元素外部点击的模式

c# - 更改 ajax 请求的 response.statuscode 引发错误 "Server cannot set status after HTTP headers have been sent"

asp.net-mvc-3 - 如何有效地从自相关表中加载数据