gpt4 book ai didi

asp.net-mvc - 在 Automapper 中修剪字符串

转载 作者:行者123 更新时间:2023-12-04 20:04:17 24 4
gpt4 key购买 nike

Mapper.CreateMap<DataViewModel, DataSource>()

我的源代码包含来自用户界面的字符串值。我想在将所有字符串映射到目标对象之前修剪所有字符串。无法为此找到解决方案。任何人都知道这是如何做到的

最佳答案

这可以使用 ForMember 来完成。方法,像这样:

Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()));

如果您想修剪多个属性,您可以链接 .ForMember()像这样的方法:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()))
.ForMember(x => x.YourString1, opt => opt.MapFrom(y => y.YourString1.Trim()))
.ForMember(x => x.YourString2, opt => opt.MapFrom(y => y.YourString2.Trim()));

虽然这可以完成工作,但我建议在应用程序的其他地方执行输入清理,因为它不属于映射。

关于asp.net-mvc - 在 Automapper 中修剪字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com