0

I am trying to return sitecore items list from my controller as list of model type

eg. public List<PageContent> Results { get; set; }

I need to get the list of items in Results.

Using glassmapper this is easily done

this.Results.Add(siteContext.GetItem<PageContent>(result.Document.GetItem().ID.Guid));

but I need to get it without glassmapper. Any way of doing this?

3
  • Create a Model and initialize it and fill model property in foreach loop Commented Aug 12, 2020 at 11:44
  • @GauravAgarwal yes created the model and filled the model but not able too add it in resuts. foreach(var res in response) { if (res != null) { this.Results.Add(res); } }
    – Karan
    Commented Aug 12, 2020 at 11:54
  • var response = results.Select(x => new PageContent { Title = !string.IsNullOrEmpty(x.Document.Title) ? x.Document.Title : string.Empty, Description = !string.IsNullOrEmpty(x.Document.Description) ? x.Document.Description : string.Empty, }).ToList();
    – Karan
    Commented Aug 12, 2020 at 11:56

1 Answer 1

1

Assuming you have list of items of type Item in results object,

ModelName model = new ModelName();

model.Results = results.Select(x => new PageContent(){
    PageContentModelProperty1 = x.Fields["TitleFieldName"]?.Value,
    PageContentModelProperty2 = x.Fields["DescriptionFieldName"]?.Value
}).ToList();

return model;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.