What I'm looking to do, is display the contents of a folder which is located on my server in a View in my MVC application.
I have what I think should be in place for the Action, however I'm a little unsure how to go about implementing the corresponding view and I was wondering if someone could point be in the right direction on that. (and also, if someone thinks my Action could be improved, advice would be welcome :) )
Here is the action:
public ActionResult Index()
{
DirectoryInfo salesFTPDirectory = null;
FileInfo[] files = null;
try
{
string salesFTPPath = "E:/ftproot/sales";
salesFTPDirectory = new DirectoryInfo(salesFTPPath);
files = salesFTPDirectory.GetFiles();
}
catch (DirectoryNotFoundException exp)
{
throw new FTPSalesFileProcessingException("Could not open the ftp directory", exp);
}
catch (IOException exp)
{
throw new FTPSalesFileProcessingException("Failed to access directory", exp);
}
files = files.OrderBy(f => f.Name).ToArray();
var salesFiles = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml");
return View(salesFiles);
}
Any help would be appreciated, thanks :)
A view is used to display data using the model class object. The Views folder contains all the view files in the ASP.NET MVC application. A controller can have one or more action methods, and each action method can return a different view.
So, for easy maintenance, the MVC framework requires a separate sub-folder for each controller with the same name as a controller, under the Views folder. For example, all the views rendered from the HomeController will resides in the Views > Home folder.
The Views folder contains all the view files in the ASP.NET MVC application. A controller can have one or more action methods, and each action method can return a different view. In short, a controller can render one or more views.
View is a User Interface which displays data and handles user interaction. Views folder contains separate folder for each controller. ASP.NET MVC supports Razor view engine in addition to traditional .aspx engine. Razor view files has .cshtml or .vbhtml extension.
If you only want the file names then you can change your Linq query to
files = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml")
.OrderBy(f => f.Name)
.Select(f => f.Name)
.ToArray();
return View(files);
Then (assuming the default project template) add the following to the Index.cshtml view
<ul>
@foreach (var name in Model) {
<li>@name</li>
}
</ul>
Which will display the list of file names
FileInfo
object, or only a file path? If the latter is true, just return a IEnumerable<string>
to the view (instead of a IEnumerable<FileInfo>
, which is what you're doing in the above code). Hint: just add a Select
call to your Linq expression...This is a simplified example of a razor view. It will ouput your file names in a HTML table.
@model IEnumerable<FileInfo>
<h1>Files</h1>
<table>
@foreach (var item in Model) {
<tr>
<td>
@item.Name
</td>
</tr>
}
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With