如何大写第一个字母剃须刀

时间:2021-03-22 21:45:50

I am new to MVC and have not found a solution for this online.

我是MVC的新手,并没有在网上找到解决方案。

I have the html as :

我把html作为:

@Html.DisplayFor(model => model.Address1) <br />

I want all the first letter of address1 to be capital letters e.g. Something Road instead of something road.

我希望地址1的第一个字母都是大写字母,例如Something Road而不是道路。

Now I have a class client and property Address1 and using EF to get the address as follow:

现在我有一个类客户端和属性Address1并使用EF获取地址如下:

 public class MyDBContext : DbContext
    {
        public DbSet<Client> Clients { get; set; }
    }

Hope it makes sense.

希望它有意义。

8 个解决方案

#1


5  

It's best to keep the presentation layer and the data access layer separate. Create a view model that wraps or translates the ORM / entity framework objects.

最好将表示层和数据访问层分开。创建包装或转换ORM /实体框架对象的视图模型。

public class ClientViewModel
{
    private Client _dao;

    public ClientViewModel(Client dao)
    {
        _dao = dao;
    }

    public string Address 
    { 
        get
        {
            // modify the address as needed here
            return _dao.Address;
        }
    }
}

#2


13  

easy solution could be

简单的解决方案可能

Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { class = "form-control" } })

then use below css to capitalize your first letter then you done.

然后使用下面的css将你的第一个字母大写,然后你就完成了。

.form-control {
text-transform:capitalize;
}

#3


6  

You could add a partial class for Client with a property that returns Address1 in title case:

您可以为Client添加一个部分类,其属性在标题大小写中返回Address1:

public partial class Client
{
    public string TitleCaseAddress1
    {
        get
        {
            return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(this.Address1);
        }
    }
}

You would then use TitleCaseAddress1 in your Razor:

然后,您将在Razor中使用TitleCaseAddress1:

@Html.DisplayFor(model => model.TitleCaseAddress1) <br />

@ Html.DisplayFor(model => model.TitleCaseAddress1)

Reference: http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.100).aspx

参考:http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.100).aspx

#4


3  

Here is the correct way to accomplish what you want, I've implemented it for you.

这是实现你想要的正确方法,我已经为你实现了它。

There is an HtmlStringFormatter.Create() which allow you to pass a delegate and make your own anonymous formatter.

有一个HtmlStringFormatter.Create(),它允许您传递委托并创建自己的匿名格式化程序。

Code Sample:

代码示例:

// This just upper case all the letters.
@Html.DisplayFormatFor(model => model.Address, HtmlStringFormatter.Create(s=> s.ToUpper()))

If You to create a custom formatter, derive from HtmlStringFormatter and set its delegate property to whatever manipulation you want to do.

如果您要创建自定义格式化程序,请从HtmlStringFormatter派生并将其delegate属性设置为您要执行的任何操作。

Code Sample:

代码示例:

// Here I use the Capital Letter custom formatter.
@Html.DisplayFormatFor(model => model.Address, new CapitalLetterFormatter())

All the classes:

所有课程:

namespace MvcPlay.HelperExtensions
{
    public static class HelperExtensions
    {
        public static MvcHtmlString DisplayFormatFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, HtmlStringFormatter formatter)
        {
            var output = helper.DisplayFor(expression);
            string formatted = formatter.Delegate.Invoke(output.ToString());
            return MvcHtmlString.Create(formatted);
        }
    }
}

namespace MvcPlay.HtmlStringFormatting
{
    public class HtmlStringFormatter
    {
        public delegate string FormatDelegate(string s);

        public FormatDelegate Delegate;
        public Expression<FormatDelegate> formatExpression;

        private HtmlStringFormatter(FormatDelegate expression)
        {
            Delegate = expression;
        }

        protected HtmlStringFormatter()
        {

        }

        public static HtmlStringFormatter Create(FormatDelegate expression)
        {
            return new HtmlStringFormatter(expression);
        }
    }

    public class CapitalLetterFormatter : HtmlStringFormatter
    {
        public CapitalLetterFormatter()
        {
            Delegate =
                s => new CultureInfo("en-US", false).TextInfo.ToTitleCase(s).ToString(CultureInfo.InvariantCulture);

        }
    }
}

Don't forget to add the following lines to the Web.Config at the Views folder:

不要忘记将以下行添加到Views文件夹中的Web.Config:

<add namespace="MvcPlay.HelperExtensions" />
<add namespace="MvcPlay.HtmlStringFormatting"/>

This will include the Formatters and the Helper Extension automatically so you won't need to include it inside every view that you want to use it in.

这将自动包括Formatters和Helper Extension,因此您不需要将其包含在要使用它的每个视图中。

#5


3  

For anyone looking to do this strictly in Razor.
This example is converting the logged in user name. Replace with your string variable.

对于任何想要在Razor中严格执行此操作的人。此示例正在转换登录的用户名。替换为您的字符串变量。

This gets first letter and converts to upper:

这得到第一个字母并转换为上限:

@(@User.Identity.GetUserName().ToString().Substring(0, 1).ToUpper())

@(@ User.Identity.GetUserName()。ToString()。Substring(0,1).ToUpper())

This gets remaining string.

这得到剩下的字符串。

@(@User.Identity.GetUserName().ToString().Substring(1, User.Identity.GetUserName().ToString().Length - 1))

@(@ User.Identity.GetUserName()。ToString()。Substring(1,User.Identity.GetUserName()。ToString()。Length - 1))

Just put them together like so to get the whole string.

只需将它们放在一起就可以获得整个字符串。

@(@User.Identity.GetUserName().ToString().Substring(0, 1).ToUpper())@(@User.Identity.GetUserName().ToString().Substring(1, User.Identity.GetUserName().ToString().Length - 1))

@(@ User.Identity.GetUserName()。的ToString()。SUBSTRING(0,1).ToUpper())@(@ User.Identity.GetUserName()。的ToString()。SUBSTRING(1,User.Identity.GetUserName ().ToString()。长度 - 1))

#6


2  

You can just use C# code for this: example from: http://www.dotnetperls.com/uppercase-first-letter

您可以使用C#代码:示例来自:http://www.dotnetperls.com/uppercase-first-letter

using System;

class Program
{
    static void Main()
    {
    Console.WriteLine(UppercaseFirst("samuel"));
    Console.WriteLine(UppercaseFirst("julia"));
    Console.WriteLine(UppercaseFirst("john smith"));
    }

    static string UppercaseFirst(string s)
    {
    // Check for empty string.
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }
    // Return char and concat substring.
    return char.ToUpper(s[0]) + s.Substring(1);
    }
}

#7


0  

I know that the original question referred to capitalizing every word in the whole string. But, I got here trying to find a solution to capitalizing the first letter of a string. I agreed with kiflay's answer as the best solution to the OP. But, expanded on it to modify only the first letter, in my usage.

我知道原始问题是指将整个字符串中的每个单词都用于大写。但是,我来到这里试图找到一个解决方案来大写字符串的第一个字母。我同意kiflay的答案是OP的最佳解决方案。但是,在我的使用中扩展它只修改第一个字母。

CSS:

CSS:

blockquote::first-letter {
text-transform: capitalize;}

#8


0  

Just simply use as follows

只需使用如下

@System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(sampleText)

#1


5  

It's best to keep the presentation layer and the data access layer separate. Create a view model that wraps or translates the ORM / entity framework objects.

最好将表示层和数据访问层分开。创建包装或转换ORM /实体框架对象的视图模型。

public class ClientViewModel
{
    private Client _dao;

    public ClientViewModel(Client dao)
    {
        _dao = dao;
    }

    public string Address 
    { 
        get
        {
            // modify the address as needed here
            return _dao.Address;
        }
    }
}

#2


13  

easy solution could be

简单的解决方案可能

Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { class = "form-control" } })

then use below css to capitalize your first letter then you done.

然后使用下面的css将你的第一个字母大写,然后你就完成了。

.form-control {
text-transform:capitalize;
}

#3


6  

You could add a partial class for Client with a property that returns Address1 in title case:

您可以为Client添加一个部分类,其属性在标题大小写中返回Address1:

public partial class Client
{
    public string TitleCaseAddress1
    {
        get
        {
            return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(this.Address1);
        }
    }
}

You would then use TitleCaseAddress1 in your Razor:

然后,您将在Razor中使用TitleCaseAddress1:

@Html.DisplayFor(model => model.TitleCaseAddress1) <br />

@ Html.DisplayFor(model => model.TitleCaseAddress1)

Reference: http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.100).aspx

参考:http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.100).aspx

#4


3  

Here is the correct way to accomplish what you want, I've implemented it for you.

这是实现你想要的正确方法,我已经为你实现了它。

There is an HtmlStringFormatter.Create() which allow you to pass a delegate and make your own anonymous formatter.

有一个HtmlStringFormatter.Create(),它允许您传递委托并创建自己的匿名格式化程序。

Code Sample:

代码示例:

// This just upper case all the letters.
@Html.DisplayFormatFor(model => model.Address, HtmlStringFormatter.Create(s=> s.ToUpper()))

If You to create a custom formatter, derive from HtmlStringFormatter and set its delegate property to whatever manipulation you want to do.

如果您要创建自定义格式化程序,请从HtmlStringFormatter派生并将其delegate属性设置为您要执行的任何操作。

Code Sample:

代码示例:

// Here I use the Capital Letter custom formatter.
@Html.DisplayFormatFor(model => model.Address, new CapitalLetterFormatter())

All the classes:

所有课程:

namespace MvcPlay.HelperExtensions
{
    public static class HelperExtensions
    {
        public static MvcHtmlString DisplayFormatFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, HtmlStringFormatter formatter)
        {
            var output = helper.DisplayFor(expression);
            string formatted = formatter.Delegate.Invoke(output.ToString());
            return MvcHtmlString.Create(formatted);
        }
    }
}

namespace MvcPlay.HtmlStringFormatting
{
    public class HtmlStringFormatter
    {
        public delegate string FormatDelegate(string s);

        public FormatDelegate Delegate;
        public Expression<FormatDelegate> formatExpression;

        private HtmlStringFormatter(FormatDelegate expression)
        {
            Delegate = expression;
        }

        protected HtmlStringFormatter()
        {

        }

        public static HtmlStringFormatter Create(FormatDelegate expression)
        {
            return new HtmlStringFormatter(expression);
        }
    }

    public class CapitalLetterFormatter : HtmlStringFormatter
    {
        public CapitalLetterFormatter()
        {
            Delegate =
                s => new CultureInfo("en-US", false).TextInfo.ToTitleCase(s).ToString(CultureInfo.InvariantCulture);

        }
    }
}

Don't forget to add the following lines to the Web.Config at the Views folder:

不要忘记将以下行添加到Views文件夹中的Web.Config:

<add namespace="MvcPlay.HelperExtensions" />
<add namespace="MvcPlay.HtmlStringFormatting"/>

This will include the Formatters and the Helper Extension automatically so you won't need to include it inside every view that you want to use it in.

这将自动包括Formatters和Helper Extension,因此您不需要将其包含在要使用它的每个视图中。

#5


3  

For anyone looking to do this strictly in Razor.
This example is converting the logged in user name. Replace with your string variable.

对于任何想要在Razor中严格执行此操作的人。此示例正在转换登录的用户名。替换为您的字符串变量。

This gets first letter and converts to upper:

这得到第一个字母并转换为上限:

@(@User.Identity.GetUserName().ToString().Substring(0, 1).ToUpper())

@(@ User.Identity.GetUserName()。ToString()。Substring(0,1).ToUpper())

This gets remaining string.

这得到剩下的字符串。

@(@User.Identity.GetUserName().ToString().Substring(1, User.Identity.GetUserName().ToString().Length - 1))

@(@ User.Identity.GetUserName()。ToString()。Substring(1,User.Identity.GetUserName()。ToString()。Length - 1))

Just put them together like so to get the whole string.

只需将它们放在一起就可以获得整个字符串。

@(@User.Identity.GetUserName().ToString().Substring(0, 1).ToUpper())@(@User.Identity.GetUserName().ToString().Substring(1, User.Identity.GetUserName().ToString().Length - 1))

@(@ User.Identity.GetUserName()。的ToString()。SUBSTRING(0,1).ToUpper())@(@ User.Identity.GetUserName()。的ToString()。SUBSTRING(1,User.Identity.GetUserName ().ToString()。长度 - 1))

#6


2  

You can just use C# code for this: example from: http://www.dotnetperls.com/uppercase-first-letter

您可以使用C#代码:示例来自:http://www.dotnetperls.com/uppercase-first-letter

using System;

class Program
{
    static void Main()
    {
    Console.WriteLine(UppercaseFirst("samuel"));
    Console.WriteLine(UppercaseFirst("julia"));
    Console.WriteLine(UppercaseFirst("john smith"));
    }

    static string UppercaseFirst(string s)
    {
    // Check for empty string.
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }
    // Return char and concat substring.
    return char.ToUpper(s[0]) + s.Substring(1);
    }
}

#7


0  

I know that the original question referred to capitalizing every word in the whole string. But, I got here trying to find a solution to capitalizing the first letter of a string. I agreed with kiflay's answer as the best solution to the OP. But, expanded on it to modify only the first letter, in my usage.

我知道原始问题是指将整个字符串中的每个单词都用于大写。但是,我来到这里试图找到一个解决方案来大写字符串的第一个字母。我同意kiflay的答案是OP的最佳解决方案。但是,在我的使用中扩展它只修改第一个字母。

CSS:

CSS:

blockquote::first-letter {
text-transform: capitalize;}

#8


0  

Just simply use as follows

只需使用如下

@System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(sampleText)