Hello guys i am trying to fetch all wwwroot images in list but it’s showing me error.
Here My Controller:
```[BindProperty]
public List<string> ImageList { get; set; }
[HttpGet]
public IActionResult OnGet()
{
var provider = new PhysicalFileProvider(WebHostEnvironment.WebRootPath);
var contents = provider.GetDirectoryContents(Path.Combine("MultiImage"));
var objFiles = contents.OrderBy(m => m.LastModified);
ImageList = new List<string>();
foreach (var item in objFiles.ToList())
{
ImageList.Add(item.Name);
}
return View("ListImages");
// return new JsonResult(objFiles);
}```
here is my view file:
```<div class="row">
<div class="col container">
<div class="col-10">
@foreach (var items in Model.ImageList)
{
var iPhotoUrl = "/MultiImage/" + items;
<img class="float-left p-2" src="@iPhotoUrl" height="150" />
}
</div>
</div>
</div>```
2
Answers
If this is Razor Pages, you should not use
return View()
.Instead your OnGet function becomes:
Option 1:
You use Mvc , so you can make a model to contain ImageList.
Below is a work demo, you can refer to it.
HomeController.cs:
Index.cshtml:
FileManagerModel
Result:
Option 2:Use Viewbag
Index.cshtml:
HomeController.cs