【问题标题】:How to pass a string into MVC model with AutoMapper如何使用 AutoMapper 将字符串传递给 MVC 模型
【发布时间】:2014-01-28 11:31:02
【问题描述】:

我正在尝试在使用 AutoMapper 生成的视图中包含一个 csv 字符串 involvedBody,但我无法确定将字符串映射到模型的位置。

我认为它会在控制器的 CreateMap 调用中或作为映射中的 .ForMember 函数,但我无法让它工作。在模型变为 IOrderedEnumerable 之前,我需要调用 InvolvedBodies 函数并将字符串传递给模型。

这是可能的还是我需要尝试其他方法?

视图模型

public partial class FamilyInterventionListViewModel
    {
        public int Id { get; set; }
        public int interventionId { get; set; }
        public string interventionCategory { get; set; }
        public string interventionType { get; set; }
        public string outcome { get; set; }
        public string achievement { get; set; }
        public string involvedBody { get; set; }
        public string startDate { get; set; }
        public string endDate { get; set; }
        public string notes { get; set; }  
    }

存储库

 public string InvolvedBodies(int interventionId)
    {
        var q = (from body in context.tEntity
                 where body.tIntervention.Any(b => b.interventionID == interventionId)
                 select body.entityName
                 );

        var ibcsv = string.Join(",", q.ToArray());                       

        return ibcsv;                    
    }

控制器

    public ActionResult InterventionType(int Id, string achievement)
    {
        var model = GetDisplay(Id)
            .Where(m => m.achievement == achievement)
            .OrderByDescending(m => m.startDate)                
            ;           

        return PartialView("_interventionType", model);
    }


//Automapper for display model
private IEnumerable<FamilyInterventionListViewModel> GetDisplay(int Id)
{
    var list = _repo.Get(Id);
    Mapper.CreateMap<tIntervention, FamilyInterventionListViewModel>();
    IEnumerable<FamilyInterventionListViewModel> viewModel = Mapper.Map <IEnumerable<tIntervention>, IEnumerable<FamilyInterventionListViewModel>>(list);            
    return viewModel;
}

映射

Mapper.CreateMap<tIntervention, FamilyInterventionListViewModel>()
    .ForMember(d => d.Id, o => o.MapFrom(s => s.interventionID))
    .ForMember(d => d.interventionCategory, o => o.MapFrom(s => s.tOutcome.tInterventionCategory.InterventionCategory))
    .ForMember(d => d.interventionType, o => o.MapFrom(s => s.tInterventionType.interventionType))
    .ForMember(d => d.outcome, o => o.MapFrom(s => s.tOutcome.outcome))
    .ForMember(d => d.achievement, o => o.MapFrom(s => s.tAchievement.achievement))
    .ForMember(d => d.involvedBody, o => o.MapFrom(s => s.tEntity.Select(m => m.entityName)))
    ;

【问题讨论】:

    标签: asp.net-mvc automapper


    【解决方案1】:

    我无法使用 Automapper 找到解决方案,因此在使用以下方法创建模型后恢复为手动映射 csv 字符串:

    foreach (var item in model)
    {
        item.involvedBody = _repo.InvolvedBodies(item.interventionId);
    }
    

    我很想知道这是否被认为是一种好的做法,或者我是否应该使用其他一些技术。

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 2012-05-12
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      相关资源
      最近更新 更多