Pedro Franco
Pedro Franco

Reputation: 2006

Mapster Adpat not working

I`m trying to make a select in form-control.

<div class="form-row form-row-wide">
     <div class="col-sm-3">
             <select asp-for="EstadoId" class="form-control input-text" required="required" placeholder="Estado"></select>
             <span asp-validation-for="EstadoId" class="text-danger"></span>
     </div>
     <div class="col-sm-9">
             <select asp-for="CidadeId" class="form-control input-text" required></select>
             <span asp-validation-for="CidadeId" class="text-danger"></span>
     </div>
</div>

But my Adapt is not getting the element`s. This is my Controller.

public async Task<JsonResult> Get(Select2Request param)
        {
            try
            {
                var pagedResults = await _service.GetAllByPageAsync(w => w.Nome.Like(param.q), 20, param.page);
                var result = pagedResults.Data.Adapt<List<StateViewModel>>().Adapt<List<Select2Response>>();

                return Json(result);
            }
            catch (Exception e)
            {
                return new JsonResult(BadRequest(e));
            }
        }

The problem is, after pass for Adpat the elements r null. This is my mapping.

public class StateMapping : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<StateViewModel, Select2Response>()
            .Map(p => p.id, s => s.EstadoId)
            .Map(p => p.text, s => s.Nome);
    }
}

How should i make it for work?

Upvotes: 2

Views: 5226

Answers (1)

Danilo P&#225;dua
Danilo P&#225;dua

Reputation: 398

At startup Startup place the following code :

using Mapster; //Reference your package


namespace YourApplication
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
           //His previous codes ...

           //Place this code
            TypeAdapterConfig.GlobalSettings.Scan(Assembly.GetEntryAssembly());
        }
    }
}

Upvotes: 3

Related Questions