How send 2 List from C# controller to view with ViewBag

Viewed 65

I am sending months (get the value from File path) and File path to the MVC view so I can show a List of the month that is linked to the File to download it like this:

 OCT(that is linked to file path)
 Nov (that is linked to file path)
 December (that is linked to file path)

This is my Controller C# code:

     public ActionResult Help()
    {

        var releaseNoteFiles = Directory.GetFiles(Server.MapPath("~/Content/ReleaseNotes"));

        List<string> month = new List<string>();
        foreach (var releaseNoteFile in releaseNoteFiles)
        {
            month.Add(new Regex("([^A-Z]*)([a-zA-Z]*)").Match(Path.GetFileNameWithoutExtension(releaseNoteFile).Split('.').Last()).Groups[2].Value);
        }
        ViewBag.releaseNoteFilesmonth = month;   /Has October,Nov,...
        ViewBag.releaseNoteFiles = releaseNoteFiles; /Has Path to the File

        return View();
    }
    

This is my view that I have a problem with how to send 2 lists (Filename and Path) This code shows the list 2 times.

          @foreach (var item in ViewBag.releaseNoteFilesmonth)
                    {foreach (var item2 in ViewBag.releaseNoteFiles)
                        {
                            @item <a href="@item2">@item</a>
                            <br />
                        }
                      }
1 Answers

It seems you want to print the month and the monthfile with hyperlink on the view, see the below code if that sorts you out.

@foreach (var item in ViewBag.releaseNoteFilesmonth)
{
    var matchedmonthfile = (ViewBag.releaseNoteFiles as string[]).Where(x => x.Contains(item)).FirstOrDefault();
    @item <a href="@matchedmonthfile">@matchedmonthfile</a>
    <br />
}
Related