Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize first letter razor

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.

like image 792
Zaki Avatar asked Oct 03 '12 16:10

Zaki


People also ask

How do I automatically capitalize the first letter?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

How do you capitalize the first letter of each word in a string?

toUpperCase() But this is not what we desire to achieve. To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you capitalize the first letter of a sentence in HTML?

text-transform:capitalize; will capitalize the first letter of a sentence, but if you want to also do it for commas you will have to write some javascript.

Do you capitalize the first letter of each word?

You should always capitalize the first letter of the first word in a sentence, no matter what the word is. Take, for example, the following sentences: The weather was beautiful. It was sunny all day. Even though the and it aren't proper nouns, they're capitalized here because they're the first words in their sentences.


2 Answers

easy solution could be

Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { class = "form-control" } })

then use below css to capitalize your first letter then you done.

.form-control {
text-transform:capitalize;
}
like image 109
kiflay Avatar answered Oct 18 '22 13:10

kiflay


You could add a partial class for Client with a property that returns Address1 in title case:

public partial class Client
{
    public string TitleCaseAddress1
    {
        get
        {
            return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(this.Address1);
        }
    }
}

You would then use TitleCaseAddress1 in your Razor:

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

Reference: http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.100).aspx

like image 6
Gromer Avatar answered Oct 18 '22 11:10

Gromer