Format string in View

Asked

Viewed 733 times

2

Is there any way to format a string (returned from a query) directly at View, to show the first uppercase letter and the rest lowercase?

Ex: In C# I can use Textinfo.Totitlecase for that reason.

string title = "war and peace";

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

At the View I know there’s a Toupper() and the Tolower(), but there is a Totitlecase or something that does the same function?

If I try to use 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 there is this method:

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

Remembering that this value returns from the database, so I need to treat the same.

  • You tried to use the ToTitleCase and it didn’t work?

  • When I try to use Totitlecase in the view, I get syntax error

  • Remembering that by JS, or other script, also meets. I would just like to know if it is possible to remove co controller.

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

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

  • toTitleCase is a method, so @Model.NmFunctioning.toTitleCase will not work. You would only access it like this if it was a property.

Show 1 more comment

2 answers

4


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.

  • Keep bringing the same is saved in the database.

  • I called the @using.System.Globalization and put the rest of the field and it worked. Thanks for the help.

  • It’s another way of doing.

  • The shape in the model I did not know, I will apply it here. Thank you very much for the help.

2

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

For example, create a statistical class with a statistical 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 "Mvcapp.Extensions" namespace, add it to the webconfig of the VIEWS FOLDER and add it to the namespace node

     <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 Voce can use anywhere in your system, be it class or Razor, the Totitlecase() method in any string:

Utilizando o metodo na view

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

It is optional to inform the culture.

  • Thank you for the @Lucasroselli reply. Really this method will be very useful in my projects.

  • I am happy to help!!! For future answers, what I lacked in my answer for it to be the best answer ?

  • 1

    I have not yet had time to test your response in my context, to find out if it really is better than the one marked earlier. For this reason I did not mark as the best yet.

  • Thank you very much for the answer!!! I look forward to any doubts!! But I believe that it is better by not global, even more without the need to replicate the code as suggested earlier, besides not changing its model.

  • @Renilsonandrade But the solution is the same as I gave, if it’s a question of encapsulating in an extension method, I can put in my answer as well.

  • @Bigown the initial doubt of my question has already been resolved, I am to analyze the answer of Lucasroselli only for educational purposes, because it was an alternative that I did not know. But if you modify your answer, I leave it as the best, to help someone who has a similar question.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.