如何从类文件中的.cshtml文件后端访问LINQ查询对象

ybzsozfc  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(116)

我要访问在ReportingConcrete类文件的messageLogs.cshtml文件中查询的日志LINQ查询对象
这个日志LINQ Query是从被查询的Razor GridView中搜索的对象(在messageLogs.cshtml中),我希望它被解析为ReportingConcrete类,以便它基于查询的对象输出Excel报告。
总结一下,我只想为Razor GridView中搜索的字段导出Excel报告。(如果有任何搜索的话)。
如果在Gridview中没有搜索,Excel报告应该从整个数据库的详细信息中生成。(我已经有了这个)。
ReportingConcrete.cs
messageLogs.cshtml.cs

try
        {
            //var logsList = await context.MessageArchive.FindAsync();
            var logs = await context.MessageArchive
                .Include(x => x.Conversation).ToListAsync();

            if (!string.IsNullOrEmpty(Request.Form["number"])) //If only number entered
            {
                NumberSearched = Convert.ToString(Request.Form["number"]);
                logs = logs.Where(x => x.PhoneNumber == NumberSearched).ToList(); //Gathers all results from thelogs
                //logsList = logs.Where(x => x.PhoneNumber == NumberSearched).to;
            }

            else if (!string.IsNullOrEmpty(Request.Form["fromDate"])) //If fromDate entered
            {
                fromDate = Convert.ToDateTime(Request.Form["fromDate"]);
                logs = logs.Where(x => x.MessageDate >= fromDate).ToList(); //Gathers all results from the fromDate

                if (!string.IsNullOrEmpty(Request.Form["toDate"])) //if form has toDate. Then logs list will include toDate parameters 
                {
                    toDate = Convert.ToDateTime(Request.Form["toDate"]);
                    logs = logs.Where(x => x.MessageDate <= toDate).ToList();
                }
            }

            else if (!string.IsNullOrEmpty(Request.Form["toDate"])) //If toDate entered
            {
                toDate = Convert.ToDateTime(Request.Form["toDate"]);
                logs = logs.Where(x => x.MessageDate <= toDate).ToList(); //Gathers all results from the toDate
            }
b09cbbtk

b09cbbtk1#

将cshtml.cs中的对象列表解析为类,然后在ReportingConcrete类中检索和访问此类。
'归档Excel类=新建归档Excel类();

var listofusers = new ArchiveExcelClass
            {
                ArchiveExcel = logs,  //.ToList()

                //ConversationId = logs.,
                //PhoneNumber = logsList.PhoneNumber,
                //MessageDate = archiveExcelViewModel.MessageDate,
                //Status = archiveExcelViewModel.Status,
                //Body = archiveExcelViewModel.Body,

            };
            return listofusers;`

 var gridSearch = _archiveExcelClass.ArchiveExcel;

        if (gridSearch != null)
        {
            foreach (var item in gridSearch)
            {
                var listofusers = (from ArchiveExcelClass in _archiveExcelClass.ArchiveExcel
                                   //_databaseContext.MessageArchive.AsNoTracking()
                                   /*var listofusers =*/
                                   select new userViewModel
                                   {
                                       ConversationId = item.Id.ToString(),
                                       PhoneNumber = item.PhoneNumber,
                                       MessageDate = item.MessageDate,
                                       Status = item.Status.ToString(),
                                       Body = item.Body,
                                   });

                return (List<userViewModel>)listofusers;   // return listofusers;
            }

相关问题