剃刀语法中的属性HTML元素(ASP MVC 3) [英] Razor syntax inside attributes of html elements (ASP MVC 3)

查看:89
本文介绍了剃刀语法中的属性HTML元素(ASP MVC 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有重复的客户行的表,我想给客户ID添加到我的表行这样的ID属性:

I have a table with repeating customer rows, I would like to add the customer ID to the ID attribute of my table rows like this:

<tr id="row<customer id>"></tr>

我尝试添加此code:

I try adding this code:

@foreach(var c in Model) {
   <tr id="row@c.id"></tr>
}

这给了我下面的输出:

Which gives me the following output:

<tr id="row@c.id"></tr>
<tr id="row@c.id"></tr>

等。

但我想它是:

<tr id="row1"></tr>
<tr id="row2"></tr>

等。

我也尝试添加&LT; TR&GT;排@ {c.id}&LT; / TR&GT; 但它没有工作..

I also tried to add <tr>row@{c.id}</tr> but it did not work..

推荐答案

你试过&LT; TR&GT;排@(c.id)LT; / TR&GT;

实际的原因,这不起作用是因为你的 row@c.id 匹配的正则表达式的一个电子邮件地址。因此,解析器假定它是一个电子邮件,而不是真正试图调用code。究其原因排@ {c.id} 不工作是因为 @ {} 不输出和是为了遏制code块。

The actual reason why this doesn't work is because your row@c.id matches the regex for an email address. So the parser assumes it's an email and not actually an attempt to call code. The reason row@{c.id} doesn't work is because the @{} doesn't output and is meant to contain blocks of code.

当有疑问你应该使用 @(),因为它会迫使什么包含在()是之间的解析为code。

When in doubt you should use @() as it will force what's contained between the () to be parsed as code.

这篇关于剃刀语法中的属性HTML元素(ASP MVC 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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