【问题标题】:Format text in datatable c#, asp.net core在数据表c#,asp.net核心中格式化文本
【发布时间】:2021-11-04 14:00:08
【问题描述】:

在 Razor 页面中查看之前,我想在控制器中将文本格式化为 Titlecase 或小写。但它不工作。我在这里做错了什么。我看不到任何效果。

public IActionResult Index()
{
    DataTable dataTable = new DataTable();
    using (SqlConnection sqlConnection = new SqlConnection(_configuration.GetConnectionString("abc")))
    {
        sqlConnection.Open();
        SqlDataAdapter sqlDa = new SqlDataAdapter("proc_Productlist", sqlConnection);
        sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
        sqlDa.Fill(dataTable);

        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;

        foreach (DataRow row in dataTable.Rows)
        {
            textInfo.ToTitleCase(row["Name"].ToString().ToLower());
            textInfo.ToLower(row["Made"].ToString().ToLower());
        }
    }
    return View(dataTable);
}

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core


    【解决方案1】:

    您没有将方法 ToTitleCaseToLower 的结果重新分配给 row["Name"]。 如果您阅读有关 ToTitleCase 的文档,它会说: 返回 指定的字符串转换为标题大小写。 你需要写:

    row["Name"] = textInfo.ToTitleCase(row["Name"].ToString().ToLower());
    row["Made"] = textInfo.ToLower(row["Made"].ToString().ToLower());
    

    祝你有美好的一天。

    来源https://docs.microsoft.com/fr-fr/dotnet/api/system.globalization.textinfo.totitlecase?view=net-5.0

    【讨论】:

    • 有道理,谢谢你的解决方案我完全忘了重新分配它。祝你有美好的一天
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2017-04-20
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多