在运行时更改视图模型的MetadataType属性 [英] changing viewmodel's MetadataType attribute at runtime

查看:293
本文介绍了在运行时更改视图模型的MetadataType属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在微软的MVC 3.0,我有一个类:

In Microsoft MVC 3.0,I have a class:

public class Product{
    public string Title {get;set;}
}

这个类可以重新present的产品或服务的,它们之间的唯一区别就是字段标签在查看时间。

This class can be represent as Product or as Service , the only difference between them is just the field labels at View time.

所以我创建了两个类:

 public class ProductMetaData
    {
        [Display(Name = "Product")]
        public object Title { get; set; }
    }

public class ServiceMetaData
    {
        [Display(Name = "Service")]
        public object Title { get; set; }
    }

我怎样才能在运行时设置这些类为 MetadataType

------------------------ -------------------编辑-------

我发现我们可以设置/更改元数据类型通过继承的 DataAnnotationsModelMetadataProvider DataAnnotationsModelValidatorProvider 类和压倒一切的 GetTypeDescriptor 方式均是这样的:

I found we can set/change metadata for a type through inheriting DataAnnotationsModelMetadataProvider and DataAnnotationsModelValidatorProvider classes and overriding GetTypeDescriptor method in both like this:

 public class xx : DataAnnotationsModelMetadataProvider
{

    protected override ICustomTypeDescriptor GetTypeDescriptor(Type type)
    {

        if (type == typeof(InvoiceItemViewModel))
            return (new AssociatedMetadataTypeTypeDescriptionProvider(typeof(InvoiceItemViewModel), typeof(InvoiceItemMetaData))).GetTypeDescriptor(type);
        else
            return base.GetTypeDescriptor(type);
    }
}

 public class yy : DataAnnotationsModelValidatorProvider
{

    protected override ICustomTypeDescriptor GetTypeDescriptor(Type type)
    {

        if (type == typeof(InvoiceItemViewModel))
            return (new AssociatedMetadataTypeTypeDescriptionProvider(typeof(InvoiceItemViewModel), typeof(InvoiceItemMetaData))).GetTypeDescriptor(type);
        else
            return base.GetTypeDescriptor(type);
    }

}

和在Global.ascx以下更改

And the following changes in Global.ascx

ModelMetadataProviders.Current = new xx();

ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new yy());

但问题是如何取决于它的模型实例并不仅仅是类型的?!......至于我看到的没有任何通过这些类的模型访问。是否有MVC pipleline任何地方改变基于模型的数据这两个供应商类新? (例如:OnActionExecuting或别的东西)

BUT the question is how can depend it on Model Instance and not just type?!...As I see there is no any access to Model through these classes. Is there any place in MVC pipleline to change these two provider classess based on Model data? (for example in : OnActionExecuting or something else?)

推荐答案

您可以写一个自定义模型元数据提供商。例如,你可以看看 MvcExtensions 。他们实施<一个href=\"http://mvcextensions.$c$cplex.com/SourceControl/changeset/view/f71bcadf0e76#Trunk/MvcExtensions/ModelMetadata/ExtendedModelMetadataProvider.cs\"相对=nofollow>为了这样提供商,以能够在运行时动态关联的元数据给定类型。所使用的 FluentValidation.NET 同样的技术。

You could write a custom model metadata provider. For example you may take a look at MvcExtensions. They implemented such provider in order to be able to dynamically associate metadata to a given type at runtime. The same technique is used by FluentValidation.NET.

这篇关于在运行时更改视图模型的MetadataType属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆