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

◇◆丶佛笑我妖孽 提交于 2019-11-29 05:28:04

问题


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 chk_@item.CustomerId.

// using the telerik grid 
id="chk_@item.OrderNumber"  // does not work 

// this will put the value of @item.Customernumber as the checkbox 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="chk_@item.Customernumber" name="@item.CustomerNumber" value="@item.OrderNumber" /></text>).Width(50)

the above will render as

<input type="checkbox" id="chk_@item.Customernumber" value=... /> 

回答1:


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




回答2:


Correct Syntax

id="@("chk_"+@item.Id)"


来源:https://stackoverflow.com/questions/4759849/how-to-concatenate-the-id-of-the-html-element-using-razor-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!