ASP.NET MVC Razor 串联 [英] ASP.NET MVC Razor Concatenation

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

问题描述

我正在尝试使用 Razor 视图引擎呈现一个如下所示的 HTML 列表:

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>

我试图用来呈现这个列表的代码是:

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_ 前缀的属性.

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

我还必须保留此语法,因为我将列表与 JQuery Sortable 和需要使用此语法格式化 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.

推荐答案

你应该用 ( ) 包裹调用的内部部分:

You should wrap the inner part of the call with ( ):

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

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

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