0

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.

1 Answer 1

1

If you want to read the data in database,you should read from dbcontext,instead of create a new instance;

in your GetAllCinimas method,you could try as below:

public IQueryable<CinimaViewModel> GetAllCinimas()
        {
            var response = _context.Cinema.Select(x => new CinimaViewModel() { Logo = x.Logo, Name = x.Name });


            return response;


        }

You could check this document related with Data Transformations with LINQ

If you have further issue, please show more details

Sign up to request clarification or add additional context in comments.

3 Comments

are there are another way without using LINQ
if there are another way without using linq please give me if please
If you want to try with Basic SQL queries, you could check this document learn.microsoft.com/en-us/ef/core/querying/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.