Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改视图模型自动映射器的映射规则_C#_Asp.net Mvc_Asp.net Mvc 3_Automapper_Automapping - Fatal编程技术网

C# 更改视图模型自动映射器的映射规则

C# 更改视图模型自动映射器的映射规则,c#,asp.net-mvc,asp.net-mvc-3,automapper,automapping,C#,Asp.net Mvc,Asp.net Mvc 3,Automapper,Automapping,我有一个模型 public class Product : BaseEntity { private string _name; public string Name { get { return _name; } private set{_name = value;} } public decimal Price { get; set; }

我有一个模型

public class Product : BaseEntity
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            private set{_name = value;}
        }

        public decimal Price { get; set; }
        public double Weight { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public double Depth { get; set; }
        public DateTime DateAdded { get; set; }
        ...
        public string GeneralInfo {get{//some get logic}...}
}
和视图模型:

public sealed class ProductDetailsModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public double Weight { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public double Depth { get; set; }
        ...
        public string GeneralInfo {get;set;}
}
和控制器:

public class ProductController : Controller
{
  ...

public ActionResult Details(int id)
        {
            var product = _productRepository.GetProduct(id);
            var productViewModel = Mapper.Map<ProductDetailsModel>(product);
            return View(productViewModel);
        }
  ...
}
一切正常,但这是错误和不合逻辑的,因为我有一个视图模型来表示产品细节,所以我想将此逻辑添加到视图模型中,但我的视图模型实例是由mapper创建的,产品视图模型没有构造函数来传递产品实例以获取其一般信息,因为,正如我所说,它不需要在构造函数中。如何将此“如果为空,则为常规信息”替换逻辑添加到映射器或视图模型中?

请尝试AfterMap

Automapper.CreateMap<Product,ProductDetailsModel>()
    .AfterMap((p,pm) => {  
       // now you have access to both objects, so you can do whatever you please
     });
Automapper.CreateMap()
.AfterMap((p,pm)=>{
//现在您可以访问这两个对象,因此您可以随心所欲
});
public class Product : BaseEntity
{
    ...
    public string GeneralInfo
        {
            get
            {
                var gInfo = getGeneralInfoFromBD();
                if (gInfo==null) 
                     gInfo = GenerateGeneralInfoFromProductProperties();
                return gInfo;
            }
            set { SetPropertyValue(ProductPropertyType.GeneralInfo, value); }
        }
}
Automapper.CreateMap<Product,ProductDetailsModel>()
    .AfterMap((p,pm) => {  
       // now you have access to both objects, so you can do whatever you please
     });