要转换的功能是使用automapper在
之下@if(Auth::user() == $post->user)
<a href="#">Edit</a>
<a href=" {{ route('delete', ['post_id' => $post->id]) }}">Delete</a>
@endif
我使用了成员的自动调用函数调用但没有成功,我甚至无法使用断点进行调试。请建议如何将此对象转换为另一个对象。我查看了许多博文,但我无法将其与我的问题联系起来。
所有我想使用automapper进行此转换,而不是创建匿名类型并使用其中的函数。
我尝试使用.forMember函数,但它不能为转换后的属性返回null。这是我的个人资料类:
public ActionResult Index()
{
Func<int,string> _roleName=t=>{return new RolesDAO().getROLEsByID(t).ROLE_NAME; };
Func<int,string> _STATENAME=t=>{return new StateDAO().getStatesByID(t).STATENAME; };
Func<int,string> _COUNTRYNAME=t=>{return new CountryDAO().getCountrieByID(t).COUNTRYNAME; };
//Func<int,string> _TYPE_NAME=t=>{return new UsersTypeDAO().getUsersTypeByID(t).TYPE_NAME; };
string usertypeName = string.Empty;
string RoleName = string.Empty;
string StateName = string.Empty;
string CountryName = string.Empty;
var value = objDAO.getUsers().Select(t => new UsersVM
{
ACTIVE=t.ACTIVE,
ROLENAME=_roleName(t.ROLEID),
PROFILE_IMAGE=t.PROFILE_IMAGE,
STATEName = _STATENAME(t.STATE),
COUNTRYName = _COUNTRYNAME(t.COUNTRY),
MOBILE=t.MOBILE,
FNAME=t.FNAME,
LNAME=t.LNAME,
EMAIL=t.EMAIL,
CITY=t.CITY,
USERTYPENAME = _TypeName(t.USER_TYPE_ID),
ADDRESS=t.ADDRESS,
}).ToList();
return View("list", value);
}
然而,它没有给出任何错误但是出乎意料的结果。自定义类型转换器也没有成功。
public class UsersDM_TO_VM : Profile
{
public UsersDM_TO_VM()
{
Func<int,string> _roleName=t=>{return new RolesDAO().getROLEsByID(t).ROLE_NAME; };
Func<int,string> _STATENAME=t=>{return new StateDAO().getStatesByID(t).STATENAME; };
Func<int,string> _COUNTRYNAME=t=>{return new CountryDAO().getCountrieByID(t).COUNTRYNAME; };
Func<int,string> _TYPE_NAME=t=>{return new UsersTypeDAO().getUsersTypeByID(t).TYPE_NAME; };
CreateMap<STBL_ADMINUSERS, UsersVM>()
.ForMember(
a => a.STATEName,
b => b.MapFrom(c => _STATENAME(c.STATE)))
.ForMember(
d => d.ROLENAME,
e => e.MapFrom(f => _roleName(f.STATE)))
.ForMember(
g => g.COUNTRYName,
h => h.MapFrom(i => _COUNTRYNAME(i.STATE)))
.ForMember(
j => j.USERTYPENAME,
k => k.MapFrom(l => _TYPE_NAME(l.STATE)));
}
}