Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalizing the first letter of an EditorFor entry

Tags:

c#

asp.net-mvc

I am trying to make it so when a user enters a value and submits it, it is stored with the first letter per word capitalized and the rest lower case. I want to do it for model.Name in:

 @Html.EditorFor(model => model.Name)

I found this neat function that does what I want, but I can't for the life of me figure out how to combine the two:

s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.toLower());

I would seriously appreciate any help, I have been working on this forever and nothing to show for it yet.

like image 481
kgst Avatar asked Jul 01 '13 23:07

kgst


1 Answers

Considering that your string is in a variable called "strSource", then you can do something like this:

char.ToUpper(strSource[0]).ToString() + strSource.Substring(1).ToLower();

Or, the better solution would be to create an extension method:

public static string ToUpperFirstLetter(this string strSource)
{
  if (string.IsNullOrEmpty(strSource)) return strSource;
  return char.ToUpper(strSource[0]).ToString() + strSource.Substring(1).ToLower();
}
like image 68
bazz Avatar answered Oct 19 '22 09:10

bazz