简体   繁体   English

在数据表 c#、asp.net core 中格式化文本

[英]Format text in datatable c#, asp.net core

I would like to format my text to Titlecase or lowercase in the controller before viewing in Razor page.在 Razor 页面中查看之前,我想在控制器中将我的文本格式化为 Titlecase 或小写。 but it is not working.但它不起作用。 what I am doing wrong here.我在这里做错了什么。 I see no effect in view.我看没有任何影响。

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);
}

You didn't reassign the result of the method ToTitleCase or ToLower to row["Name"] .您没有将方法ToTitleCaseToLower的结果重新分配给row["Name"] If you read the documentation about ToTitleCase it says: Returns The specified string converted to title case.如果您阅读有关ToTitleCase的文档,它会说:返回已转换为标题大小写的指定字符串。 You need to write:你需要写:

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

Have a nice day.祝你今天过得愉快。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM