字符串长度属性

发布于 2024-09-30 06:07:25 字数 252 浏览 2 评论 0原文

我正在使用 Asp.Net MVC 2,并尝试迭代元数据以将输入控件添加到网格列。默认模型元数据提供程序不会填充大多数属性(例如 DisplayAttribute、StringLength 属性等)。

1-我认为 MVC3 将支持这些属性,对吗?

2-是否有一个自定义提供程序可以在 MVC3 发布之前使用,我记得看到过一个自定义元数据提供程序(以为它在 MVCContrib 中),但在那里找不到它,有人知道在哪里可以找到支持此属性的元数据提供程序吗?

I am using Asp.Net MVC 2, and trying to iterate through the metadata to add input controls to grid columns. Most attributes like DisplayAttribute, StringLength attribute, etc are not populated by the default modelmetadata provider.

1- I think these attributes are going to be supported in MVC3, right?

2- Is there a custom provider that I can use till MVC3 is out, I remember seeing a custom metadata provider (thought it was in MVCContrib) but could not find it there, anyone knows where to find the metadataprovider supporting this attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-10-07 06:07:25

我编写了一个提供程序来处理比标准提供程序更多的属性。想法是这样的:

/// <summary>
/// Adds support for data annotation attributes omitted from DataAnnotationsModelMetadataProvider
/// </summary>
public class ExtendedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var result = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        DisplayAttribute da = attributes.OfType<DisplayAttribute>().FirstOrDefault();
        if (da != null)
        {
            var autoGenerate = da.GetAutoGenerateFilter();
            if (autoGenerate.HasValue)
            {
                result.AdditionalValues[AdditionalValuesKeys.AutoGenerateFilter] = autoGenerate.Value;
            }
            var groupName = da.GroupName;
            if (!string.IsNullOrEmpty(groupName))
            {
                result.AdditionalValues[AdditionalValuesKeys.GroupName] = groupName;
            }
            if (!string.IsNullOrEmpty(da.Prompt))
            {
                result.Watermark = da.Prompt;
            }
        }

        DisplayColumnAttribute dc = attributes.OfType<DisplayColumnAttribute>().FirstOrDefault();
        if (dc != null)
        {
            var sc = dc.SortColumn;
            if (!string.IsNullOrEmpty(sc))
            {
                result.AdditionalValues[AdditionalValuesKeys.SortColumnName] = sc;
                if (dc.SortDescending)
                {
                    result.AdditionalValues[AdditionalValuesKeys.SortDescending] = true;
                }
            }
        }

        StringLengthAttribute sla = attributes.OfType<StringLengthAttribute>().FirstOrDefault();
        if (sla != null)
        {
            result.AdditionalValues[AdditionalValuesKeys.MaximumStringLength] = sla.MaximumLength;
        }

        return result;
    }
}

当然,MVC 的默认模板实际上不会对该信息执行任何操作。您必须 自己定制它们

I wrote a provider to handle more attributes than the standard provider does. Here's the idea:

/// <summary>
/// Adds support for data annotation attributes omitted from DataAnnotationsModelMetadataProvider
/// </summary>
public class ExtendedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var result = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        DisplayAttribute da = attributes.OfType<DisplayAttribute>().FirstOrDefault();
        if (da != null)
        {
            var autoGenerate = da.GetAutoGenerateFilter();
            if (autoGenerate.HasValue)
            {
                result.AdditionalValues[AdditionalValuesKeys.AutoGenerateFilter] = autoGenerate.Value;
            }
            var groupName = da.GroupName;
            if (!string.IsNullOrEmpty(groupName))
            {
                result.AdditionalValues[AdditionalValuesKeys.GroupName] = groupName;
            }
            if (!string.IsNullOrEmpty(da.Prompt))
            {
                result.Watermark = da.Prompt;
            }
        }

        DisplayColumnAttribute dc = attributes.OfType<DisplayColumnAttribute>().FirstOrDefault();
        if (dc != null)
        {
            var sc = dc.SortColumn;
            if (!string.IsNullOrEmpty(sc))
            {
                result.AdditionalValues[AdditionalValuesKeys.SortColumnName] = sc;
                if (dc.SortDescending)
                {
                    result.AdditionalValues[AdditionalValuesKeys.SortDescending] = true;
                }
            }
        }

        StringLengthAttribute sla = attributes.OfType<StringLengthAttribute>().FirstOrDefault();
        if (sla != null)
        {
            result.AdditionalValues[AdditionalValuesKeys.MaximumStringLength] = sla.MaximumLength;
        }

        return result;
    }
}

Naturally, the default templates with MVC don't actually do anything with that info. You have to customize them yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文