Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I working on .net 5 mvc i have table name cinimas have 5 rows .

my issue I can't display these 5 rows on custom view model .

I try but no rows display

what I try

1- model creation

C#
public class Cinema:IEntityBase
    {
        [Key]
        public int Id { get; set; }

        [Display(Name = "Cinema Logo")]
        [Required(ErrorMessage = "Cinema logo is required")]
        public string Logo { get; set; }

        [Display(Name = "Cinema Name")]
        [Required(ErrorMessage = "Cinema name is required")]
        public string Name { get; set; }

       
    }

2-create custom model view that will display data as view models from table .

C#
public class CinimaViewModels
    {
        public string Logo { get; set; }
        public string Name { get; set; }
    }

3-create service Interface ICinemasService have defintion for data will display .

C#
public interface ICinemasService:IEntityBaseRepository<Cinema>
    {
        IQueryable<CinimaViewModels>GetAllCinimas();
    }


on step number 4 no rows returned although table have 5 rows .

so how to solve this issue

I need to return data on custom view model CinimaViewModels as Queryable but nothing display

so How solve this issue?

I check this to return data from model direct it return 5 rows as below as below .

var cinmas2 = _context.Cinemas.ToList();
but from view model on step 4 as below no rows displayed .

What I have tried:

4- implement interface on CinemasService class

public class CinemasService:EntityBaseRepository<Cinema>, ICinemasService
    {
        private readonly AppDbContext _context;
        public CinemasService(AppDbContext context) : base(context)
        {
            _context = context;
        }
     public  IQueryable<CinimaViewModels> GetAllCinimas()
        {
            var cinmas = new Cinema();
         
            var response = new List<CinimaViewModels>
            {
                new CinimaViewModels 
                {
            Description=cinmas.Description,
           Logo=cinmas.Logo,
           Name=cinmas.Name
                }
          
            };
            return response.AsQueryable();

            
        }
}
Posted
Updated 4-Dec-22 22:26pm

1 solution

Quote:
I check this to return data from model direct it return 5 rows as below as below:
C#
var cinmas2 = _context.Cinemas.ToList();

but from view model on step 4 as below no rows displayed:
C#
public IQueryable<CinimaViewModels> GetAllCinimas()
{
    var cinmas = new Cinema();
    var response = new List<CinimaViewModels>
    {
        new CinimaViewModels 
        {
            Description=cinmas.Description,
            Logo=cinmas.Logo,
            Name=cinmas.Name
        }
    };
    return response.AsQueryable();
}
So you have one piece of code which works to return your five cinemas. But you chose to ignore that, and instead create a list containing a single CinimaViewModels instance copied from a blank Cinema instance.

And you seriously can't see what the problem is?

Use the working code that you already have to get the Cinema instances. Then use that to populate your sequence of CinimaViewModels instances.
C#
public IQueryable<CinimaViewModels> GetAllCinimas()
{
    return _context.Cinemas.Select(cinema => new CinimaViewModels
    {
        Description = cinema.Description,
        Logo = cinema.Logo,
        Name = cinema.Name,
    });
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900