Format string in View

2

Is there any way to format a string (returned from a query) directly into View , to display the first letter of the letter and the lower case?

Ex: In C # I can use the TextInfo.ToTitleCase for this.

string title = "war and peace";

TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
title = textInfo.ToTitleCase(title); //War And Peace

In the View I know there is ToUpper () and ToLower () , but there is a ToTitleCase or something that does the same function?

If I try to use it directly in the view:

    @model PortalRH.DomainModel.Entities.Usuario
<div class="container">
    <div class="col-md-8">
        <table class="table">
            <tr>
                <td bgcolor="#ffffff" align="right"><strong>Endereço:</strong></td>
                <td bgcolor="#ffffff"> @Model.NmFuncionario.toTitleCase</td>
            </tr>
        </table>
    </div>
</div>

I get an error saying that this method exists:

  

'string' does not contain a definition for 'ToTitleCase' and no extension method.

Remembering that this value returns from the database, then I need to treat it.

    
asked by anonymous 04.02.2015 / 19:21

2 answers

3

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.

    
04.02.2015 / 19:43
2

Renilson, another suggestion would be to create an extension class, that is, so you can create several methods by extending the existing classes:

For example, create a static class with a static method:

  namespace MVCApp.Extensions
  {
     public static class StringExtensions
     {
        public static string ToTitleCase(this string valor, string cultura = null)
        {
           if (string.IsNullOrWhiteSpace(cultura))
              cultura = "en-US";

           TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
           return textInfo.ToTitleCase(valor);
        }
     }
  }

Take the namespace "MVCApp.Extensions", and add it in the webconfig of the VIEWS PASTE and add to the namespace

     <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="MVCApp.Extensions" />
     </namespaces>

Now build the project, close and open the visual studio Ready! Now you can use the ToTitleCase () method anywhere in your system, either class or razor, in any string:

Remember, any string in your project can now use this method

It is optional to inform the culture.

    
10.03.2015 / 18:13