Search code examples
razorrazor-2

Wrap each newline (/n) with <li> </li>


I have a small project which i have a question for. Users fill out a form and after submitting it i wish to wrap each new line in the textarea with <li></li>.

So basically i have the following code.

<textarea name="Questions"></textarea>

On the other submit.cshtml i wish to wrap each newline with <li></li> before the email is sent out. So the email will contain the following

  • Question 1
  • Question 2
  • Question 3
  • Question 4

the information in the textarea gets stored in a variable called Questions I wish to break that variables each new line into <li></li> and store the new value in a second variable called EmailBody to be included in an email.

I know how to do this i PhP but in Razor view engine i'm kind of at a loss. Thanks in advance!

UPDATE Solved it by modifying Aron's code to this.

    foreach (var line in Questions.Split('\n'))
    {
        EmailBody = EmailBody + "<li>" + @line + "</li>";
    }  

Solution

  • @foreach(var line in Model.Text.Split(Environment.NewLine)){
        <li>@line</li>
    }