【问题标题】:The {0} must be at least {2} characters long{0} 必须至少有 {2} 个字符长
【发布时间】:2015-01-12 21:06:42
【问题描述】:

我有一个 Visual Studio 2013 MVC Razor 项目,我正在通过浏览w3schools.com 上的一个示例来研究该项目。

在ASP.NET MVC Security 章节中,您将在 Models 类中看到默认文件 AccountModels.cs,其中每个 密码 字段的文本如下:

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }

我熟悉String.Format,其中参数必须从0开始并递增。

然而,上面的第二个参数跳转到 2,并且似乎没有足够的参数传递给字符串。

在学习项目时,我会尽我所能自定义功能(如字符串响应)以更好地强化我的学习。

这是怎么回事?

【问题讨论】:

标签: asp.net-mvc razor visual-studio-2013


【解决方案1】:

我们基本上是在调用一个类的构造函数:

StringLengthAttribute 是一个类,它的构造函数有一个名为int 的参数maximumLength

还有一些Properties:

  • ErrorMessage 类型为 string
    • 在我们的例子中是字符串:"The {0} must be at least {2} characters long."
  • ErrorMessageResourceName 类型为 string
    • 在我们的例子中,我们不给它任何价值
  • ErrorMessageResourceType 类型为 System.Type
    • 在我们的例子中,我们不给它任何价值
  • MinimumLength 类型为 int
    • 在我们的例子中,我们给它赋值6

ErrorMessageResourceName 字符串(还)与其他属性无关,所以它不是这样的:

String.Format("some variable {0} and some other {1}...", 100, 6)

所以数字100 和属性MinimumLength = 6 是根本不是(尚未)发送以使用字符串"The {0} must be at least {2} characters long." 格式化的参数。

StringLengthAttribute 类也有一些方法,其中之一叫做FormatErrorMessage

这个方法在内部调用来格式化消息,它在内部使用String.Format格式化字符串,这里是当参数被传递给字符串被正确格式化的时候。

  • {0} 已绑定到 DisplayName
  • {1} 绑定到最大长度
  • {2} 绑定到 MinimumLength

这是内部调用的方法(如果你想知道它是如何在内部调用的):

/// <summary>
    /// Override of <see cref="ValidationAttribute.FormatErrorMessage"/>
    /// </summary>
    /// <param name="name">The name to include in the formatted string</param>
    /// <returns>A localized string to describe the maximum acceptable length</returns>
    /// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception>
    public override string FormatErrorMessage(string name) {
        this.EnsureLegalLengths();

        bool useErrorMessageWithMinimum = this.MinimumLength != 0 && !this.CustomErrorMessageSet;

        string errorMessage = useErrorMessageWithMinimum ?
            DataAnnotationsResources.StringLengthAttribute_ValidationErrorIncludingMinimum : this.ErrorMessageString;

        // it's ok to pass in the minLength even for the error message without a {2} param since String.Format will just
        // ignore extra arguments
        return String.Format(CultureInfo.CurrentCulture, errorMessage, name, this.MaximumLength, this.MinimumLength);
    }

参考资料:

  • 微软参考源here
  • 关于 Stackoverflow 的类似问题:“stringlength 属性 errormessage 采用哪些参数?” here
  • StringLengthAttribute Class here 的几乎没用的 msdn 文档

【讨论】:

  • 您的回答与我的回答大致相同,但之所以获奖是因为它更详细。谢谢卡斯塔诺先生。
【解决方案2】:

经过一番调查,我在ASP.NET Forum by CodeHobo上找到了一个答案:

你可以在这里找到完整的文档

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx

[StringLength(100, ErrorMessage = "密码必须至少有 {0} 个字符", MinimumLength = 6)]

在这种情况下,错误消息只是呈现时应用的字符串模板。想想一个string.Format。所以相当于

string.Format("{0} 必须至少有 {2} 个字符长。",DisplayName,MaximumLength,MinimumLength);

0索引是属性的显示名称,1是最大长度,2是最小长度

对于您的示例,这将显示显示名称而不是最小长度。您需要将 {0} 更改为 {2}

[StringLength(100, ErrorMessage = "密码必须至少有 {0} 个字符", MinimumLength = 6)]

是的,我本可以简单地删除我的问题,但 SO 是我的主要编程信息来源。

如果我在这里找不到编程答案,我觉得它需要它。

我还没有完全理解 100% 的答案,所以如果有人有更好的答案,我很乐意接受。

【讨论】:

  • 这在此处进行了一些讨论:stackoverflow.com/questions/9540314/…,但您是正确的,格式字符串的结构并未真正包含在 MSDN 中。
  • 这也让我很困惑。非常感谢您回答您自己的问题而不是删除它。我通过 Google 搜索找到了您的问题和答案。
猜你喜欢
  • 2022-08-21
  • 1970-01-01
  • 2011-07-28
  • 2012-11-05
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
相关资源
最近更新 更多