I working on an ASP.NET Core 5 MVC app. I have a table cinimas with 5 rows.
My issue I can't display these 5 rows in my custom view model.
I try but no rows are displayed.
What I tried:
Step 1: model creation
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; }
}
Step 2: create custom model view that will display data as view models from table:
public class CinimaViewModels
{
public string Logo { get; set; }
public string Name { get; set; }
}
Step 3: create service interface ICinemasService with definition for data display:
public interface ICinemasService:IEntityBaseRepository<Cinema>
{
IQueryable<CinimaViewModels>GetAllCinimas();
}
Step 4: implement that interface in 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();
}
}
In step 4, no are rows returned although table does have 5 rows.
So how to solve this issue?
I need to return data on custom view model CinimaViewModels as IQueryable but nothing is displayed.
How to solve this issue?
Update: I checked this to return data from model it return 5 rows as below:
var cinmas2 = _context.Cinemas.ToList();
but from view model on step 4 as above nothing is returned.