tags:

views:

68

answers:

2

Hello SO:

I have this view in an ASP.NET MVC application:

<%
    var path = Path.Combine(HttpRuntime.AppDomainAppPath, "uploads"); 
    foreach (var file in Directory.GetFiles(path).OrderBy(f => new FileInfo(f).Length))
    {
        var item = new FileInfo(file);
%>
<tr>
    <td></td>
    <td>
        <%=Html.Encode(Path.GetFileName(item.Name))%>
    </td>
    <td>
        <%=Html.Encode(Functions.FormatBytes(item.Length))%>
    </td>
    <td>
        <%=Html.FileLink(item.Name)%>
    </td>
</tr>
<% } %>

Is it possible to access my variable f inside the loop, or is there some other way to do this so I do not have to dimension two instances of FileInfo(file)?

Thanks!

+6  A: 
var fileInfos = new DirectoryInfo(path).GetFiles().OrderBy(f => f.Length);

foreach (var fileInfo in fileInfos)
{
   ...
}
dtb
ack, the simplest solutions always elude me. thanks :)
Anders
+2  A: 

Your view is really overstepping it's responsibilities here. You should create a class that maps to what data you want to show in the view, then in your controller, retrieve the data and populate a IEnumerable<> of your classes and return that in the ViewModel, which your view can then just simple loop through.

John Sheehan
Thanks john, I will do this. I am still learning MVC, so I appreciate your critiques :)
Anders