The ideal is to put these things on the model or controller when relevant. The view should be reserved only to assemble the presentation. If you need to have the die with the first capitalized letters then you should have a property, probably in the template, that delivers the die this way to you. This is the right thing to do. 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);
}
}
There 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 doesn’t work the way you want but the code you want works elsewhere, bring the code that works to the view, thus:
@{
var textInfo = new System.Globalization.CultureInfo("en-US", false).TextInfo;
this.Write(textInfo.ToTitleCase("war and peace"));
}
Another way to facilitate the use of this algorithm is to create a utility method, so just call it without having to write a 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);
}
}
}
Then you can call very simply on view:
@Model.NmFuncionario.ToTitleCase()
And it is possible to pass as a parameter of this method a different culture from the American one. In fact you can change the extension method to leave by default the culture you intend to use more as the "en".
To use this method would have to put in the beginning @using.Extensions
. If you want to automatically make available for all pages, what is desirable in most cases should put the following line within the tag <namespaces>
in the archive web.config
of your project:
<add namespace="Extensions" />
I put in the Github for future reference.
But I still prefer to do on the model.
You tried to use the
ToTitleCase
and it didn’t work?– Maniero
When I try to use Totitlecase in the view, I get syntax error
– Randrade
Remembering that by JS, or other script, also meets. I would just like to know if it is possible to remove co controller.
– Randrade
Edit the question and post all relevant information about it. If the error is syntax we need to see how you are using and exactly what the error is. Ideally let the view with the minimum possible processing, whenever possible it is better to place processing on the model or controller.
– Maniero
@bigown I edited the answer with a small example from View. I’m wondering if there is a way to do it in the View, because I think it would make it a little faster, like adding a Toupper(). Please correct me if I’m wrong.
– Randrade
toTitleCase is a method, so @Model.NmFunctioning.toTitleCase will not work. You would only access it like this if it was a property.
– Renan