如何使用Betty的方法使用AutoMapper将DataAnnotation元数据传输到ViewModel [英] How to Transfer DataAnnotation metadata to ViewModel with AutoMapper with Betty's Approach

查看:110
本文介绍了如何使用Betty的方法使用AutoMapper将DataAnnotation元数据传输到ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要澄清如何实现Betty的代码解决方案,以使用AutoMapper将数据注释元数据传输到ViewModel(请参见此处) .或者,如果您有更好的方法,请分享.对于熟悉AutoMapper的人来说,Betty答案的实现也许是显而易见的,但我是新手.

I need clarification on how to implement Betty's code solution to transferring data annotation metadata to ViewModels with AutoMapper (see here). Or if you have a better way, please share that. Maybe the implementation of Betty's answer is obvious to someone who knows AutoMapper well, but I'm new to it.

这是一个简单的示例,我如何添加此代码以使Betty的解决方案起作用:

Here is a simple example, what do I add to this code to make Betty's solution work:

// Data model Entity
public class User1
{

    [Required]
    public int Id { get; set; }

    [Required]
    [StringLength(60)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(60)]
    public string LastName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [StringLength(40)]
    public string Password { get; set; }

}

// ViewModel
public class UserViewModel
{

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Password { get; set; }

}

当前的AutoMapper实现:

Current AutoMapper Implementation:

// Called once somewhere
Mapper.CreateMap<User1, UserViewModel>(MemberList.Destination);

// Called in controller method, or wherever
User user = new User() { FirstName = "Tony", LastName = "Baloney", Password = "secret", Id = 10 };

UserViewModel userVM = Mapper.Map<User, UserViewModel>(user);

// NOW WHAT??? 

我已经在Application_Start的global.asax中尝试过此操作:

I've tried this in global.asax in Application_Start:

var configProvider = Mapper.Configuration as IConfigurationProvider;
ModelMetadataProviders.Current = new MetadataProvider(configProvider);
ModelValidatorProviders.Providers.Clear(); // everything's broke when this is not done
ModelValidatorProviders.Providers.Add(new ValidatorProvider(configProvider));

此外,我还必须从以下位置修改Betty的GetMappedAttributes:

Also, I had to modify Betty's GetMappedAttributes from:

propertyMap.DestinationProperty.GetCustomAttributes可以: propertyMap.DestinationProperty.MemberInfo.GetCustomAttributes

(或者代替MemberInfo,它是MemberType吗?)甚至可以构建.

(or instead of MemberInfo, is it MemberType?) for this to even build.

但是似乎没有任何作用.

But nothing seems to work.

推荐答案

Automapper不使用元数据提供程序,而是使用Automapper.

The metadata provider isn't used by Automapper, it uses Automapper.

您不需要直接调用它,只要您在Global.asax.cs中启动时向MVC注册,它就会由MVC自动调用,例如:

You don't need to call it directly, it's automatically called by MVC as long as you register it with MVC on startup in Global.asax.cs, for example:

ModelMetadataProviders.Current = new MetadataProvider(
        AutoMapper.Mapper.Engine.ConfigurationProvider);

ModelValidatorProviders.Providers.Add(new ValidatorProvider(
        AutoMapper.Mapper.Engine.ConfigurationProvider);

或:

ModelMetadataProviders.Current = new MetadataProvider(
        (AutoMapper.IConfigurationProvider)AutoMapper.Mapper.Configuration);

ModelValidatorProviders.Providers.Add(new ValidatorProvider(
        (AutoMapper.IConfigurationProvider)AutoMapper.Mapper.Configuration));

这篇关于如何使用Betty的方法使用AutoMapper将DataAnnotation元数据传输到ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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