ASP.NET MVC 通过下拉列表显示类内容
在我的 NHIbernate (数据库模型)中,我有这个:
public class Pers {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public string City{ get; set ;}
public int Age{ get; set ;}
public Role Role{ get; set ;}
}
我有一些 dropwon (数据库模式):
public class Role {
public int Id{ get; set ;}
public string NL{ get; set ;}
public string FR{ get; set ;}
}
在我看来,我想使用下拉列表并显示 < 的一些记录(不是全部,在我的真实类中有更多属性)代码>个人。我为 Pers 创建了一个 Dto 类,其中包含我需要的字段:
public class PersDto {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
public class RoleDto {
public int Id{ get; set ;}
public string NL{ get; set ;}
public string FR{ get; set ;}
}
在控制器中:
Mapper.CreateMap<Role, RoleDto>();
myModel.RoleDto = Mapper.Map<Role, RoleDto>(roleListFromDB);
Mapper.CreateMap<Pers, PersDto>();
myModel.PersDto = Mapper.Map<Pers, PersDto>(persFromDB);
public class MyModel{
public PersDto PersDto{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
这是正确的方法吗?或者最好通过创建一个 PersDto
来做到这一点:
public class MyModel{
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
自动映射器是否可以仅复制某些字段而不是全部?
谢谢,
In my NHIbernate (Database Model) I have this :
public class Pers {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public string City{ get; set ;}
public int Age{ get; set ;}
public Role Role{ get; set ;}
}
I have some dropwon (Database mode) :
public class Role {
public int Id{ get; set ;}
public string NL{ get; set ;}
public string FR{ get; set ;}
}
In my view I'd like use the dropdown and display some record (not all, in my real class there are much more properties) of Pers
. I created a Dto class for Pers with the fields I need :
public class PersDto {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
public class RoleDto {
public int Id{ get; set ;}
public string NL{ get; set ;}
public string FR{ get; set ;}
}
In the controller :
Mapper.CreateMap<Role, RoleDto>();
myModel.RoleDto = Mapper.Map<Role, RoleDto>(roleListFromDB);
Mapper.CreateMap<Pers, PersDto>();
myModel.PersDto = Mapper.Map<Pers, PersDto>(persFromDB);
public class MyModel{
public PersDto PersDto{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
Is it the right way ? Or it's better to do this with creating a PersDto
:
public class MyModel{
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
public RoleDto RoleDto{ get; set ;}
}
Is it possible with automapper to copy only some fields and not all ?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不应在控制器中调用
Mapper.CreateMap
。在 AppDomain 的整个生命周期中,最好仅在Application_Start
中调用此方法。您可以编写一个映射配置文件:
然后在 Application_Start 中配置这些配置文件:
最后在您的控制器中仅使用
Mapper.Map
方法:No, you should not call
Mapper.CreateMap<TSource, TDest>
in the controller. This method should be invoked only once for the entire lifetime of the AppDomain, ideally inApplication_Start
.You could write a mapping profile:
then in Application_Start configure those profiles:
and finally in your controller only use the
Mapper.Map<TSource, TDest>
method: