Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation with ASP.NET MVC3 Razor

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="[email protected]" />

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

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

like image 940
Flavio CF Oliveira Avatar asked Nov 07 '11 16:11

Flavio CF Oliveira


4 Answers

Just put your variable next to prefix:

<input type="checkbox" id="chk@(obj.field)" />
like image 138
Samich Avatar answered Nov 08 '22 15:11

Samich


Try

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

or

<input type="checkbox" id="[email protected]" />
like image 43
Stanislav Ageev Avatar answered Nov 08 '22 13:11

Stanislav Ageev


<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")
like image 5
Zandro Avatar answered Nov 08 '22 15:11

Zandro


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

like image 3
DanielB Avatar answered Nov 08 '22 15:11

DanielB