Ideally, you should put these things in the model or controller where appropriate. The view should be reserved only to mount the presentation. If you need to have the die with the first letters capitalized then you should have a property, probably in the model, that delivers the data in this way to you. This is the most correct. I'm not saying that doing the right thing is always desirable.
I would consider creating a property like this:
public string TitleCaseNmFuncionario {
get {
return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(this.NmFuncionario);
}
}
Then in the view you can use:
@Model.TitleCaseNmFuncionario
But if you want to do everything in the view, it is possible but you have to call the method with the correct syntax:
@System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Model.NmFuncionario)
If this does not work the way you want but the code you want works elsewhere, bring the code that works for view , like this:
@{
var textInfo = new System.Globalization.CultureInfo("en-US", false).TextInfo;
this.Write(textInfo.ToTitleCase("war and peace"));
}
Another way to make it easier to use this algorithm is to create a utility method, so you simply call it without having to write long code that is not ideal in a view:
namespace Extensions {
public static class StringExtensions {
public static string ToTitleCase(this string texto, string cultura = "en-US") {
if (string.IsNullOrWhiteSpace(cultura)) {
cultura = "en-US";
}
var textInfo = new System.Globalization.CultureInfo(cultura, false).TextInfo;
return textInfo.ToTitleCase(texto);
}
}
}
Here you can call very simply view :
@Model.NmFuncionario.ToTitleCase()
And it is possible to pass as a parameter of this method a culture different from the American one. In fact you can change the extension method to leave by default the culture you intend to use more like "en-US".
To use this method you would have to put @using.Extensions
on top. If you want to automatically make it available to all pages, it is desirable in most cases to place the following line within the tag <namespaces>
in the web.config
file of your project:
<add namespace="Extensions" />
But I still prefer to do it in the template.