How to capitalize first letter razor

2019-06-18 03:58发布

I am new to MVC and have not found a solution for this online.

I have the html as :

@Html.DisplayFor(model => model.Address1) <br />

I want all the first letter of address1 to be capital letters e.g. Something Road instead of something road.

Now I have a class client and property Address1 and using EF to get the address as follow:

 public class MyDBContext : DbContext
    {
        public DbSet<Client> Clients { get; set; }
    }

Hope it makes sense.

8条回答
Juvenile、少年°
2楼-- · 2019-06-18 04:35

Just simply use as follows

@System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(sampleText)
查看更多
Melony?
3楼-- · 2019-06-18 04:40

You can just use C# code for this: example from: http://www.dotnetperls.com/uppercase-first-letter

using System;

class Program
{
    static void Main()
    {
    Console.WriteLine(UppercaseFirst("samuel"));
    Console.WriteLine(UppercaseFirst("julia"));
    Console.WriteLine(UppercaseFirst("john smith"));
    }

    static string UppercaseFirst(string s)
    {
    // Check for empty string.
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }
    // Return char and concat substring.
    return char.ToUpper(s[0]) + s.Substring(1);
    }
}
查看更多
登录 后发表回答