String concatenation with ASP.NET MVC3 Razor

笑着哭i 提交于 2019-11-30 22:25:00

问题


i'm trying to concatenate a string in asp.net mvc 3 razor and i'm getting a little sintax problem with my cshtml.

i what to generate an id for my checkboxes on a foreach statement, and my checkboxes should start with "chk" and what to cancatenate a fieldon the ID, something like that:

<input type="checkbox" id="chk+@obj.field" />

but or exampple the result for id attribute is: id="chk+8"

how can i just get a result for something like "chk8"?


回答1:


Just put your variable next to prefix:

<input type="checkbox" id="chk@(obj.field)" />



回答2:


Try

<input type="checkbox" id="@("chk" + obj.field)" />

or

<input type="checkbox" id="chk@obj.field" />



回答3:


<input type="checkbox" id="chk@(obj.field)" /> should work.

The most direct and clean way to add a prefix a suffix.

@("PREFIX " + obj.field + " SUFFIX")



回答4:


<input type="checkbox" id="chk@(obj.field)" /> should work.



来源:https://stackoverflow.com/questions/8039131/string-concatenation-with-asp-net-mvc3-razor

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