Andy Schneider
Andy Schneider

Reputation: 8684

Dyanmically create Hyperlinks to all files in a folder using MVC 3

I have a controller that returns a list of files in a directory using DirectoryInfo.GetFiles()

public ActionResult Index()
        { 
            DirectoryInfo vidDir = null;
            FileInfo[] files = null;

            string dirPath = @"/Content/Videos";
            vidDir = new DirectoryInfo(Server.MapPath(dirPath));
            files = vidDir.GetFiles();

            return View(files);
        }

In my view I enumerate the files with the following code:

<ul>  @foreach (var file in Model) {
                  <li>
                   @file 
                  </li> 
            }
                  </ul>

What I would like to do is foreach (var file in Model) { create a hyperlink to file }

I was able to hard code a link using @html.actionlink

 @Html.ActionLink("Test Link", "file1.txt", "Content/Videos");

When I put in @file for the first two parameters, I get an error that says:

'System.Web.Mvc.HtmlHelper' has no applicable method named 'ActionLink' but appears to have an extension method by that name"

How can I use @html.Actionlink to create a hyperlink to all the files passed into the view?

Upvotes: 1

Views: 3160

Answers (2)

fluidguid
fluidguid

Reputation: 1681

I am using below code and it works just fine

foreach (var menuName in menuNames)
{
    @Html.ActionLink(menuName.Value, menuName.ActionName, menuName.ControllerName)
}

(I am using this in MVC 4)

Upvotes: 0

amit_g
amit_g

Reputation: 31250

These seem to be a direct link to those files. If so

<a href="@Url.Content(string.Concat("Content/Videos/", @file))">@file</a>

Upvotes: 3

Related Questions