将EditorFor条目的第一个字母大写

kgs*_*gst 6 c# asp.net-mvc

我试图这样做,当用户输入一个值并提交它时,它存储的每个单词的首字母大写,其余的小写.我想为model.Name做:

 @Html.EditorFor(model => model.Name)
Run Code Online (Sandbox Code Playgroud)

我发现这个整洁的功能可以做我想要的,但我不能为我的生活弄清楚如何将两者结合起来:

s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.toLower());
Run Code Online (Sandbox Code Playgroud)

我会非常感谢任何帮助,我一直在努力解决这个问题.

baz*_*azz 2

考虑到您的字符串位于名为“strSource”的变量中,那么您可以执行以下操作:

char.ToUpper(strSource[0]).ToString() + strSource.Substring(1).ToLower();
Run Code Online (Sandbox Code Playgroud)

或者,更好的解决方案是创建一个扩展方法:

public static string ToUpperFirstLetter(this string strSource)
{
  if (string.IsNullOrEmpty(strSource)) return strSource;
  return char.ToUpper(strSource[0]).ToString() + strSource.Substring(1).ToLower();
}
Run Code Online (Sandbox Code Playgroud)