0

I need to list all ceremonies incase of my searchModel parameters are empty. So When the page loaded, we should have a list of multimedia albums of ceremonies. The following method doing generate this list. but I need to show items of my MultimediaViewList in RazorPage one by one. So I should access to the following items and its values in HTML part of my razor page Id,Title,CeremonyId,Ceremony,FileAddress How can I do that?

public Dictionary<string, List<MultimediaViewModel>> Search(MultimediaSearchModel searchModel)
{
    var query = _hContext.Multimedias.Include(x => x.Ceremony).Select(g => new MultimediaViewModel
    {
        Id = g.Id,
        Title = g.Title,
        CeremonyId = g.CeremonyId,
        Ceremony = g.Ceremony.Title,
        FileAddress = g.FileAddress
    }).GroupBy(g => g.CeremonyId).ToList();
    return query.ToDictionary(k => k.Key, v => v.ToList());
}

this is My View Model:

public class MultimediaViewModel
    {
        public long Id { get; set; }
        public string Title { get;  set; }
        public string FileAddress { get;  set; }
        public long CeremonyId { get;  set; }
        public string Ceremony { get; set; }
    }
Amin
  • 13
  • 5

1 Answers1

0

If I understood you correctly, I think the return type of your method should be a List of MultimediaViewModel, and then return query.SelectMany(x=> x).Distinct().ToList() Also, preferablly you should assign the return value of this method to a property called MultimediaViewList to use it in your razor page.

  • As explained, the return type of search method is Dictionary>. Now I need to know how I can display the items of MultimediaViewModel for example in a foreach loop in my razor page. – Amin Dec 05 '20 at 14:27
  • `foreach(var KeyValuePair in Model.Search(Model.MultimediaSearchModel){ foreach(var item in keyValuePair.Value){

    @item.Title

    } }`
    – George Marcus Dec 05 '20 at 15:50
  • Sorry but it is not the thing that I want. let me explain more. assume that I have a 10 records. these records reduced through Group by to 6 records. Now I need to make a list including JUST 6 records not 10 records. your answer make a list including Group by items within its childs that is totally 10 records. So I need the MultimediaViewModel items(Id, Title, FileAddress,...) just for Key items not for values of each key item. – Amin Dec 06 '20 at 04:40
  • I think with the use of SelectMany() and Distinct() on the result of your search you would get 6 records. `foreach(var KeyValuePair in Model.Search(Model.MultimediaSearchModel).SelectMany(x=> x).Distinct()){

    @item.Title

    }`
    – George Marcus Dec 06 '20 at 15:52