Search code examples
asp.net-mvcnhibernate-mapping

error while mapping class to SelectListItem


I'm trying to map a class to SelectListItem like this :

return Json(id == null
                ? Enumerable.Empty<SelectListItem>()
                : Find<BusLineRouteStop>.All.Where(x => x.Template.Id == id)
                    .MapTo<BusLineRouteStop, SelectListItem>());  

And getting this:

Error: Missing type map configuration or unsupported mapping. Mapping types: BusLineRouteStop -> SelectListItem


Solution

  • This happens because you haven't specified the mapping rule (system doesn't know which values of BusLineRouteStop are corresponding to values of SelectListItem. You can do it in another way, if this is one-time event:

    return Json(id == null
                    ? Enumerable.Empty<SelectListItem>()
                    : Find<BusLineRouteStop>.All.Where(x => x.Template.Id == id).Select(x => new SelectListItem
                    {
                        Value = x.BusLineStopValue,
                        Text = x.BusLineStopName,
                        Disabled = false,
                        Selected = false
                    });
    

    P.S. I have used the imaginary fields of BusLineRouteStop to demonstrate the opportunities.