ASP.NET MVC剃刀级联 [英] ASP.NET MVC Razor Concatenation

查看:100
本文介绍了ASP.NET MVC剃刀级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图渲染HTML列表看起来像以下,使用的Razor视图引擎:

I'm trying the render an HTML list that looks like the following, using the Razor view engine:

<ul>
  <li id="item_1">Item 1</li>
  <li id="item_2">Item 2</li>
</ul>

在code,我试图用它来呈现这个列表是:

The code that I am attempting to use to render this list is:

<ul>
@foreach (var item in Model.TheItems)
{            
  <li id="item_@item.TheItemId">Item @item.TheItemId</li>
}
</ul>

解析器是窒息,因为它认为一切都在id属性下划线的右边是纯文本,不应该被解析。我不确定如何指示解析器来呈现TheItemId。

The parser is choking, because it thinks that that everything to the right of the underscore in the id attribute is plain text and should not be parsed. I'm uncertain of how to instruct the parser to render TheItemId.

我不想但包含ITEM_ preFIX模型对象上的属性

I don't want to but a property on the model object that includes the item_ prefix.

我也有保持这种语法,我使用列表使用jQuery可排序,最适合需要id属性在这个语法要格式化的序列化功能。

I also have to keep this syntax as I am using the list with JQuery Sortable and with the serialize function that requires the id attribute to be formatted in this syntax.

推荐答案

您应该包装呼叫的内部()

<li id="item_@(item.TheItemId)">

这篇关于ASP.NET MVC剃刀级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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