【问题标题】:Displaying a string array in Razor Pages?在 Razor Pages 中显示字符串数组?
【发布时间】:2020-07-27 17:44:53
【问题描述】:

我正在尝试将字符串打印为 Razor 页面中的列表。我有以下内容:

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>

页面一直在一个字符串中打印出该行。我也尝试使用无序列/有序列表作为 HTML 标记,但我无法使其工作。

【问题讨论】:

    标签: visual-studio razor razor-pages


    【解决方案1】:

    需要其他反斜杠所以 \n 而不是 /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>
    

    【讨论】:

    • Np 犯过类似的错误一百万次
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2012-07-29
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多