2

I'm trying to print out a string as a list in a Razor Page. I have the following:

Aboud.cshtml.cs:

public void OnGet()
{
    Message = "Hello";

    Message = AddToMessage(new string[] {
        "This is my first page",
        "By the end of this course",
        "I will be a Razor Pages Pro!"});


}


private string AddToMessage(string[] text)
{
    string result = "";
    foreach (string txt in text)
    {
        result += txt + "\n";
    }

    result = ReplaceToUpper(result);
    return result;
}


private string ReplaceToUpper(string text)
{
    return text.ToUpper();
}

About.cshtml:

@page
@model AboutModel
@{
}

<h1>This is the about page!</h1>
<br />
<p>@{
    string[] message = @Model.Message.Split("/n");
    foreach(string text in message)
    {
        <br /><p>@text</p>;
    }

   }
</p>

The page keeps printing out the line in one string. I've also tried using an unordered/ordered list as the HTML markup and I'm not able to make it work.

1 Answer 1

2

Need other backslash so \n instead of /n

@page
@model AboutModel
@{
}

<h1>This is the about page!</h1>
<br />
<p>@{
    string[] message = @Model.Message.Split("\n");
    foreach(string text in message)
    {
        <br /><p>@text</p>;
    }

   }
</p>
Sign up to request clarification or add additional context in comments.

1 Comment

Np made similar mistake a million times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.