开发者

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

开发者 https://www.devze.com 2023-02-05 19:06 出处:网络
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.

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开发者_如何学Go" value=... /> 


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)


Correct Syntax

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


This does works now in latest MVC:

id="chk_@item.OrderNumber"

Happy coding.

0

精彩评论

暂无评论...
验证码 换一张
取 消