3
public ViewResult Details(string strap)
{
    var parcel = _service.GetProperty(strap);
    var owners = _service.GetOwners(parcel);
    var other_characteristics = _service.GetOtherCharacteristics(parcel);
    var viewModel = new PropertyViewModel();

    Mapper.Map(parcel, viewModel);
    Mapper.Map(owners, viewModel.Owners);
    Mapper.Map(other_characteristics.DELINEATED_DISTRICT, viewModel.DELINEATED_DISTRICT);


    return View(viewModel);
}

我觉得我应该能够设置我的代码以允许这成为我需要编写的全部内容。AutoMapper 将处理剩下的事情。或者也许写得更少。

    Mapper.Map(parcel, viewModel);
    Mapper.Map(owners, viewModel);
    Mapper.Map(other_characteristics.DELINEATED_DISTRICT, viewModel);

这是我重写的 Configure() 方法。在我首先写这个问题时,我还没有添加 DELINEATED_DISTRICT 的映射。

   protected override void Configure()
    {
     //TODO: Add Mappings from Models to Data Objects 


        CreateMap<Property, PropertyViewModel>();
        CreateMap<IEnumerable<Owner>, PropertyViewModel>();
        //Names are the same in domain and viewModel


    }

视图模型

      public class PropertyViewModel
        {

            public PropertyViewModel()
            {
                Owners = new List<Owner>();
                Characteristics = new List<Characteristics>();

            }


            public string STRAP { get; set; }
            public string PROPERTY_ID { get { return String.Format("{0}-{1}-{2}", STRAP.Substring(0, 4), STRAP.Substring(4, 2), STRAP.Substring(6, 4)); }  }
            public string SITUS { get; set; }
            public string MAILING_ADDRESS { get; set; }
            public string PROPERTY_USE { get; set; }
            public string SUBDIVISION { get; set; }
            public string DSCR { get; set; }
            public string LAND_AREA { get; set; }
            public string INCORPORATION { get; set; }
            public string SEC_TWP_RGE { get; set; }
            public string CENSUS { get; set; }
            public string DELINEATED_DISTRICT { get; set; }

            public IList<Owner> Owners { get; set; }
            public IList<Characteristic> Characteristics {get;set;}

        }

楷模

public class Property
{
    [Key]        
    public string STRAP { get; set; }
    public string PROPERTY_ID {get;set;}
    public string SITUS { get; set; }
    public string MAILING_ADDRESS { get; set; }
    public string PROPERTY_USE { get; set; }
    public string SUBDIVISION { get; set; }
    public string DSCR { get; set; }
    public string LAND_AREA { get; set; }
    public string INCORPORATION { get; set; }
    public string SEC_TWP_RGE { get; set; }
    public string CENSUS { get; set; }
}

public class Owner
{
    [Key]
    public string OWNER_ID {get;set;}
    public string STRAP {get;set;}
    public string NAME {get;set;}

}
4

1 回答 1

0

您必须使用 Mapper.Map(parcel, viewModel);映射没有其他方式。对不起。

于 2012-05-29T22:20:27.287 回答