C# 创建模型以保存路径的结果。合并数组
我的开发环境是VS19、ASP.NETCoreRazor(非MVC)3.1页和实体框架核心 在“模型”文件夹中,我有以下类/模型,它有一个属性:C# 创建模型以保存路径的结果。合并数组,c#,asp.net,asp.net-core,razor,razor-pages,C#,Asp.net,Asp.net Core,Razor,Razor Pages,我的开发环境是VS19、ASP.NETCoreRazor(非MVC)3.1页和实体框架核心 在“模型”文件夹中,我有以下类/模型,它有一个属性: ```` public class FilesObject { public string[] Files { get; set; } = new string[1000]; } 在index.cs文件的OnGet()处理程序中,我编写了以下代码以返回一个数组,该数组包含指定目录(wwwroot\documents)中的文件列表 我收到以下错
````
public class FilesObject
{
public string[] Files { get; set; } = new string[1000];
}
在index.cs文件的OnGet()处理程序中,我编写了以下代码以返回一个数组,该数组包含指定目录(wwwroot\documents)中的文件列表
我收到以下错误:错误CS0029无法将此行上的类型字符串隐式转换为Models.FilesObject:
FilesObject = Directory.GetFiles(docsPath);
我最初尝试过这样做:
var DocumentInfo=Path.Combine(webRootPath,“documents”)
这很好,但是,我无法将DocumentInfo返回到index.cshtml页面来处理它,比如ForEach循环
我明白错误告诉我的是什么,我只是不知道如何修复它
谢谢你的帮助。提前感谢您抽出时间。要获取目录中的文件列表,请执行以下操作:
DirectoryInfo d = new DirectoryInfo(@"path/that/you/are/interested/in");
FileInfo[] Files = d.GetFiles("*.txt"); // e.g. getting Text files
看
及
错误CS0029无法将此行上的类型字符串隐式转换为Models.FilesObject:
FilesObject = Directory.GetFiles(docsPath);
Directory.GetFiles
返回string[]
,因此需要进行如下更改:
FilesObject.Files = Directory.GetFiles(docsPath);
在使用它之前,您需要初始化fileobject
FilesObject.Files = Directory.GetFiles(docsPath);
1.Index.cshtml:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
@foreach(var data in Model.FilesObject.Files)
{
<div>
@data;
</div>
}
结果:
Path.Combine
返回一个字符串
,它只是给定路径的一个良好格式字符串。在我看来,您正在等待目录中的文件列表?抱歉,我在上面的源代码中复制了不正确的代码。上面的源代码不正确,已经更正。您好@1Zero3Tech,如果我的答案对您有帮助,您能接受我的答案吗?