Search code examples
c#asp.netasp.net-mvcrazorrazorengine

razor output int to DOM and increment in same line


Ok, this is really bugging me!

I'm trying to do the following in one line:

This works:

<h4>3.1.@skillSectionNumber SKILLS</h4>
{skillSectionNumber++;}

This doesn't work:

<h4>3.1.@skillSectionNumber++ SKILLS</h4>

This doesn't work:

<h4>3.1.@{Response.Write(skillSectionNumber++.ToString());} SKILLS</h4> 

Cheers


My solution was this:

<h4>3.1.@(skillSectionNumber++) SKILLS</h4>

I was missing the parens.

Thanks


Solution

  • You can use pre-increment approach.

    @{ var skillSectionNumber = 10;}
    <h4> 3.1.@(++skillSectionNumber) SKILLS </h4>
    

    It basically first increase the value of you skillSectionNumber variable and then use it (for rendering)