14

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"?

Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63

5 Answers5

39

Just put your variable next to prefix:

<input type="checkbox" id="chk@(obj.field)" />
Samich
  • 29,157
  • 6
  • 68
  • 77
10

Try

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

or

<input type="checkbox" id="chk@obj.field" />
Stanislav Ageev
  • 778
  • 4
  • 10
5

<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")
Tim M.
  • 53,671
  • 14
  • 120
  • 163
Zandro
  • 51
  • 1
  • 1
3

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

DanielB
  • 19,910
  • 2
  • 44
  • 50
0

Best way to concate any C# variable in rozer view by using string.Format

id="@string.Format("{0}_Title", _Id)" // Apend after
id="@string.Format("Title_{0}", _Id)" // Apend before
id="@string.Format("Title_{0}_Title", _Id)" // Apend Middle
Kalpesh Dabhi
  • 760
  • 8
  • 18