N. Sch N. Sch - 10 months ago 42
C# Question

Putting together first a variable and then text

I have a form I want the name of the input to be the day of the week the variable represents and then Beg so for example if it represents Sunday it should be SundayBeg.

name='@l.Day"+ Beg"'

How do I do this?

Answer Source

If you just want to put it into a string like your example, and assuming that l.Day is the value you want to concatenate with "Beg"...

var name = $"{l.Day}Beg";

EDIT: It's been pointed out that string interpolation does not play nice in Razor views. I am leaving this example here for posterity, but removing it from my example below.

Or if you're not using C#6...

var name = string.Format("{0}Beg", l.Day);

Then, you just display the value of name wherever you wish. Based on your example, I'm guessing you want to use the value as the name property of some HTML element. So you would do it like so:

<input name="@name" ... />

You could also just do it all in-line in place of using a variable.

<input name="@string.Format("{0}Beg", l.Day)" ... />