asp.net-mvc 如何使用 Razor ASp.NET MVC 连接 HTML 元素的 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4759849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:46:20  来源:igfitidea点击:

How to concatenate the id of the HTML element using Razor ASp.NET MVC

asp.net-mvctelerikasp.net-mvc-3razor

提问by johndoe

I have a grid column with checkboxes and I want to give them a different id. Id is based on the CustomerId in the Model. What syntax should I use to concatenate the [email protected].

我有一个带有复选框的网格列,我想给它们一个不同的 ID。Id 基于模型中的 CustomerId。我应该使用什么语法来连接 [email protected]

// using the telerik grid 
id="[email protected]"  // does not work 

// this will put the value of @item.Customernumber as the checkbox id

// 这会将@item.Customernumber 的值作为复选框ID

columns.Template(@<text><input type='checkbox' id="@item.Customernumber" name="@item.CustomerNumber" value="@item.OrderNumber" /></text>).Width(50)

second option:

第二种选择:

columns.Template(@<text><input type='checkbox' id="[email protected]" name="@item.CustomerNumber" value="@item.OrderNumber" /></text>).Width(50)

the above will render as

以上将呈现为

<input type="checkbox" id="[email protected]" value=... /> 

回答by Omu

[email protected]will not work because razor thinks of it as of an e-mail, you need to do it like this instead: chk_@(item.OrderNumber)

[email protected]将不起作用,因为 razor 将其视为电子邮件,您需要这样做: chk_@(item.OrderNumber)

回答by Saboor Awan

Correct Syntax

正确的语法

id="@("chk_"[email protected])"