Home > Mobile >  Response a model with AutoMapper in ASP.NET
Response a model with AutoMapper in ASP.NET

Time:04-11

I'm have an API that response a stuffs null entity which default generated by scaffold but I want it return as my TutorRequestModel.

Api response for 'My service file'

My service file

public PageList<Entity> GetAll(TutorRequestParams param)
{
    var listAll = _repository.GetAll();
    CheckDataNotNull("shaped", listAll);

    Search(ref listAll, param);

    var sortedStudent = _sortHelper.ApplySort(listAll, param.OrderBy);
    var shapedOwners = _dataShaper.ShapeData(sortedStudent, param.Fields);
    
    return PageList<Entity>.ToPageList(shapedOwners, param.PageNume, param.PageSize);
}

But I want it response with my QuestResponseModel like

public class TutorRequestModel
{
   public int TutorRequestId { get; set; }
   public string Title { get; set; }
   public string Description { get; set; }
   public string Status { get; set; }
   public int? StudentId { get; set; }
   public int? GradeId { get; set; }
   public int? SubjectId { get; set; }
}

I have tried with Auto Mapper

_mapper.Map<IEnumerable<TutorRequestModel>>(shapedData);

It works, but when I try to shape my data, only the field that I shaped have a value while the rest are null:

public PageList<TutorRequestModel> GetAll(TutorRequestParams param)
{
    var listAll = _repository.GetAll();
    CheckDataNotNull("shaped", listAll);

    Search(ref listAll, param);

    var sortedStudent = _sortHelper.ApplySort(listAll, param.OrderBy);
    var shapedOwners = _dataShaper.ShapeData(sortedStudent, param.Fields);
    var rs = _mapper.Map<IEnumerable<TutorRequestModel>>(shapedOwners);
    return PageList<TutorRequestModel>.ToPageList(rs, param.PageNume, param.PageSize);
}

There is any way to return as TutorRequestModel and also can shaped data

CodePudding user response:

Try put [JsonIgonre] in the entity class

  • Related