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

时间:2022-12-06 19:35:35

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:

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

 @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.

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

3 个解决方案

#1


2  

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

考虑到你的字符串在一个名为“strSource”的变量中,你可以这样做:

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

#2


0  

An option would be to make a custom EditorTemplate (Views -> Shared -> EditorTemplates)

一个选项是制作一个自定义的EditorTemplate(Views - > Shared - > EditorTemplates)

TitleString.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.String>" %>
<%=Html.TextBox("", System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Model.ToLower()))%>

And then in your view where you want that formatting you can do something like:

然后在您的视图中,您希望格式化,您可以执行以下操作:

@Html.EditorFor(model => model.Name, "TitleString")

For more details check out: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

有关详细信息,请访问:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

#3


0  

You can capitalize the first letter of each word according to CultureInfo by simply using this on the Controller:

您只需在Controller上使用它,即可根据CultureInfo将每个单词的首字母大写:

Note: "test" is a sample property returned from the View (as Name, Surname, Address, etc.)

注意:“test”是从View返回的样本属性(如姓名,姓氏,地址等)

text = string.IsNullOrEmpty(text) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower(new CultureInfo("tr-TR", false)));

Please note that, in here there is an extra control for null values. Hope this helps...

请注意,在这里有一个额外的空值控制。希望这可以帮助...

#1


2  

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

考虑到你的字符串在一个名为“strSource”的变量中,你可以这样做:

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

#2


0  

An option would be to make a custom EditorTemplate (Views -> Shared -> EditorTemplates)

一个选项是制作一个自定义的EditorTemplate(Views - > Shared - > EditorTemplates)

TitleString.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.String>" %>
<%=Html.TextBox("", System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Model.ToLower()))%>

And then in your view where you want that formatting you can do something like:

然后在您的视图中,您希望格式化,您可以执行以下操作:

@Html.EditorFor(model => model.Name, "TitleString")

For more details check out: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

有关详细信息,请访问:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

#3


0  

You can capitalize the first letter of each word according to CultureInfo by simply using this on the Controller:

您只需在Controller上使用它,即可根据CultureInfo将每个单词的首字母大写:

Note: "test" is a sample property returned from the View (as Name, Surname, Address, etc.)

注意:“test”是从View返回的样本属性(如姓名,姓氏,地址等)

text = string.IsNullOrEmpty(text) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower(new CultureInfo("tr-TR", false)));

Please note that, in here there is an extra control for null values. Hope this helps...

请注意,在这里有一个额外的空值控制。希望这可以帮助...