简体   繁体   English

当在 ASP.NET Core 5 MVC 中显示表中的数据时,视图 model 没有任何显示,尽管表有多行?

[英]When display data from table in ASP.NET Core 5 MVC on view model nothing display although table have multiple rows?

I working on an ASP.NET Core 5 MVC app.我正在开发 ASP.NET Core 5 MVC 应用程序。 I have a table cinimas with 5 rows.我有一个 5 行的桌子cinimas

My issue I can't display these 5 rows in my custom view model.我的问题是我无法在自定义视图 model 中显示这 5 行。

I try but no rows are displayed.我试过了,但没有显示任何行。

What I tried:我尝试了什么:

Step 1 : model creation第一步:model创建

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:第 2 步:创建自定义 model 视图,它将数据显示为表中的视图模型:

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

Step 3 : create service interface ICinemasService with definition for data display:第 3 步:创建服务接口ICinemasService并定义数据显示:

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

Step 4 : implement that interface in CinemasService class第 4 步:在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.在第 4 步中,虽然表确实有 5 行,但没有返回任何行。

So how to solve this issue?那么如何解决这个问题呢?

I need to return data on custom view model CinimaViewModels as IQueryable but nothing is displayed.我需要将自定义视图 model CinimaViewModels上的数据返回为IQueryable ,但未显示任何内容。

How to solve this issue?如何解决这个问题?

Update : I checked this to return data from model it return 5 rows as below:更新:我检查了这个以从 model 返回数据,它返回 5 行,如下所示:

var cinmas2 = _context.Cinemas.ToList();

but from view model on step 4 as above nothing is returned.但是从上面步骤 4 的视图 model 来看,没有返回任何内容。

If you want to read the data in database,you should read from dbcontext,instead of create a new instance;如果要读取数据库中的数据,应该从dbcontext中读取,而不是创建一个新的实例;

in your GetAllCinimas method,you could try as below:在你的 GetAllCinimas 方法中,你可以尝试如下:

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您可以使用 LINQ 查看与数据转换相关的文档

If you have further issue, please show more details如果您还有其他问题,请显示更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM