Using the first letter of the editor to write

I try to do this when the user enters a value and sends it, it is stored with the first letter in the headword and the rest lower case. I want to do this for model.Name in:

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

I found this neat feature that does what I want, but I can’t understand for life how to combine the two:

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

I would really appreciate any help, I worked on it forever and haven’t shown anything yet.

+6
source share
3 answers

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

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

Or a 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
source

The parameter would be to create a custom 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 this formatting, you can do something like:

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

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

0
source

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

Note: "test" is a sample property returned from the view (like first name, last name, address, etc.).

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

Note that there is an additional control for null values ​​here. Hope this helps ...

0
source

Source: https://habr.com/ru/post/948508/


All Articles